Why isn't sys.objects available as objects and what are default schemas being searched for objects by SQL Server?
Upon reading INFORMATION_SCHEMA vs sysobjects , I wanted to ask the question:
SQL Server 2005/2008 - Why is the sys.sysobjects view available to users without the schema name?
which seems to be already asked but I do not find that the answers answered the question(asked).Why sysbjects is available as sys.sysobjects if sys schema did not exist in earlier versions of SQL Server?
(the explanation was that it is for backward compatibility with earlier versions of SQL Server)Then, the answer by Barry:
SQL Server looks for 开发者_如何学Pythonobjects in the following order:
- sys schema
- users schema (This is different for Stored Procs - it will look in the stroed procs schema rather than the users schema.
- dbo schema
has left pending the question by Shaun in comments:
Why sys.objects is not queriable without "sys." pefix (as objects)?What are default schemas being searched by SQL Server and in which order?
The user's default schema is searched, and then the dbo schema is searched. The sys schema is never searched, unless it's explicitly specified (e.g. sys.objects).
However, for the backward compatibility views (e.g. sysobjects) there is some special logic - the schema, even if specified, is ignored, and the view from the sys schema is used. Since older code would never create a table called "dbo.sysobjects", this isn't an issue, because these views are present for backwards compatibility, and should not be used for any future development.
Fun and games with sysobjects:
create table dbo.sysobjects (
ID int not null
)
go
select * from dbo.sysobjects
go
(For those who don't want to run it - you still get the full sysobjects output, and not an empty table with one column).
精彩评论