SQL same column name allowed

后端 未结 4 1623
Happy的楠姐
Happy的楠姐 2021-01-11 15:38

When I run this query in SQL Server

SELECT custid AAA, companyname AAA
FROM Sales.Customers
WHERE country = \'USA\'

It\'s running fine. But

4条回答
  •  梦谈多话
    2021-01-11 16:15

    This can be explained by understanding order of execution of different logical phases of query execution. Query Execution Order MSDN

    In SQL Server the order is FROM > WHERE > SELECT i.e. first FROM clause is executed then WHERE clause and last is the SELECT list.

    Now in your first query , all matching rows from table Sales.Customers are fetched and then then afterwards columns specified in SELECT list are pulled out and then Alias names are applied.

    In your second query , the inner query is executed successfully as the first query but when the outer query's FROM clause tries to fetch columns from resultset returned by inner query , it finds duplicate columns and throws error.

提交回复
热议问题