SQL Server - Return SCHEMA for sysobjects

后端 未结 8 1430
旧巷少年郎
旧巷少年郎 2020-12-29 21:30

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

相关标签:
8条回答
  • 2020-12-29 22:25

    Just to repeat what's already been suggested here, here's what I've used, to get a list of Tables, Stored Procedures, Views and Functions in my database:

    SELECT schema_Name(schema_id)  as  SchemaName,
           [name],              --  Name of the Table, Stored Procedure or Function
           [type]               --  'V' for Views, 'U' for Table, 'P' for Stored Procedure, 'FN' for function
    FROM sys.objects 
    WHERE [type_desc] IN ( 'USER_TABLE', 'SQL_STORED_PROCEDURE', 'VIEW', 'SQL_SCALAR_FUNCTION')
    AND [name] NOT LIKE 'sp_%'
    AND [name] NOT LIKE 'fn_%'
    ORDER BY 3 DESC,        --  type first
            1 ASC,          --  then schema
            2 ASC           --  then function/table name
    

    ...and here's what our good friend Northwind would return...

    0 讨论(0)
  • 2020-12-29 22:29

    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.

    0 讨论(0)
提交回复
热议问题