views:

4728

answers:

4

How to I get the SCHEMA when doing a select on sysobjects?

I am modifing a stored procedure called SearchObjectsForText which returns only the Name but I would also like to include the SCHEMA.

Right now it is doing something similar to this:

SELECT DISTINCT name
FROM sysobjects

I would like to know what tables need to be joined to return the SCHEME for each 'name'.

+5  A: 

If you mean SQL Server 2005 or higher, use sys.objects instead of sysobjects:

SELECT  sys.objects.name, sys.schemas.name AS schema_name
FROM    sys.objects 
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id

2005 introduced schemas. up to 2000, users equaled schemas. The same query for SQL Server 2000:

SELECT  sysusers.name AS OwnerName, sysobjects.name
FROM sysobjects
INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
devio
Because of the layout of the SearchObjectsForText stored procedure I ended up using this method. But I also liked "wire science" because it was much simplier.
Gerhard Weiss
+1  A: 
bdukes
I wish I could but I do not want to change the existing stored procedure to much and just doing a join would be easier. Thanks for the suggestion anyway. I gave you an up-vote for it. :)
Gerhard Weiss
Do not use INFORMATION_SCHEMA views to determine the schema of an object?Vow! To me this sounds like a bug that must be fixed, not like a feature that is documented in MSDN. If you agree, make sure to submit feedback on MSDN site.
AlexKuznetsov
+3  A: 

On Sql Server 2005 (and above) you can use the sys.objects view:

select 
  name                    as  ObjectName,     
  schema_Name(schema_id)  as  SchemaName
from 
  sys.objects

In Sql Server 2000 (and below), "schema" had a different conceptual meaning. Note from MSDN:

In earlier releases of SQL Server, databases could contain an entity called a "schema", but that entity was effectively a database user. SQL Server 2005 is the first release of SQL Server in which a schema is both a container and a namespace.

wire science
+2  A: 

I would favor using the more focused "sys" views - sys.procedures instead of sys.objects. You'll need to join it with the sys.schemas view to get schema name and such.

select
    p.name, 
    s.name 'Schema',
    p.type_desc, p.create_date, p.modify_date
from
    sys.procedures p
inner join
    sys.schemas s ON p.schema_id = s.schema_id

I would start to get away from using "sysobjects" since Microsoft clearly states in Books Online that "sysobjects" is subject to removal in a future release:

This SQL Server 2000 system table is included as a view for backward compatibility. We recommend that you use the current SQL Server system views instead. To find the equivalent system view or views, see Mapping SQL Server 2000 System Tables to SQL Server 2005 System Views. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

Marc

marc_s
Thanks for the idea. It is a pretty big procedure but one that will have to be reviewed. It is using all the old sys views.
Gerhard Weiss