SQL Server Object Names

岁酱吖の 提交于 2019-12-05 10:29:39

The SQL Server supports muliti-part identifiers:

linked_server.db_name.schema.table_name

In your case you have:

Select *
From Sch1.Table_1 
Join Sch2.Table_1
    on Sch1.Table_1.Id = Sch2.Table_1.Id

Now you wonder why SQL Server cannot differentiate between them:

Sch1.Table_1  != Sch2.Table_1

The case is because of SQL Server use something called exposed name.

exposed name

which is the last part of the multi-part table name (if there is no alias), or alias name when present

Returning to your query you have exposed names Table_1 and Table_1 which are duplicates and you need to use aliases.

From SQL Server 2005+:

Duplicate table detection algorithm has been changed correspondingly, so that any tables with the same exposed names will be considered duplicates

I suspect that your code could work with SQL Server 2000 but I cannot check it for sure.

For more info read Msg 1013

As far as I can tell, I don't see any errors in your sample code. Please explain in detail what errors you're encountering.

As for the four-part naming convention. the full object name syntax is:

server.database.schema.object

So a complete usage would be, for example:

select * from servername.databasename.Sch1.Table_1

or

select * from servername.databasename.Sch2.Table_2

from which you can ignore any part as long as there is no ambiguity. Therefore in your example you can ignore severname and databasename as they are the same. But you cannot ignore schema names as they are not.

Addendum:

Based on error message you posted later, you need to employ correlation naming on the join syntax:

select * 
from Sch1.Table_1 as t1
inner join Sch2.Table_1 as t2 on t1.ID=t2.ID
   Select *
    From Sch1.Table_1 x
    Join Sch2.Table_1 y
        on x.Id = y.Id

Does this work?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!