开发者

Why isn't sys.objects available as objects and what are default schemas being searched for objects by SQL Server?

开发者 https://www.devze.com 2023-01-29 05:36 出处:网络
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?

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:

  1. sys schema
  2. users schema (This is different for Stored Procs - it will look in the stroed procs schema rather than the users schema.
  3. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消