SQL same column name allowed

后端 未结 4 1627
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:25

    Reason for this problem is:

    1st Query

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

    Here, you are assigning column alias at the time of output, so AAA is as column name(alias actually) attached at the time of returning result, but i guess you will see only 1st column with AAA other will be removed because of possible confliction during further reference. so here you are not getting error.


    2nd Query

    SELECT * FROM (
        SELECT custid AAA, companyname AAA
        FROM Sales.Customers
        WHERE country = 'USA') BBB
    

    Here you got error because, you are selecting records from Inline View with name BBB, here that Inner Query(inline view) is considered as a Table(for your SELECT * FROM statement), and as we know - basically Table can not have multiple same column name, because of that you are getting error that BBB has multiple AAA column.

提交回复
热议问题