Friday , March 29 2024

Ambiguous column name Error and SQL Server Alias(TSQL)

 

When reading data from tables, we may need to give different names to tables and columns. We can do this by defining alias to columns or tables.

In our first example we will give different names to the tables.

Example1:

We usually need to give Alias when we join the tables.

First, let’s create two tables as below and add a few records to these tables.

Then try to run the following join query.

When we run the query we will get the error as follows.

Msg 209, Level 16, State 1, Line 17

Ambiguous column name ‘ID’.

The reason for the error is that the ID column exists in both tables.

In this case, the problem will be solved when we write the query using alias. We can use alias as follows.

In the second example we will describe the alias to the columns.

Example2:

When joining the first table and the second table, combine the name and surname with the + operator. As a result, let’s give the column a different name with an alias.

 

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories