SQL Server - Return SCHEMA for sysobjects

后端 未结 8 1432
旧巷少年郎
旧巷少年郎 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:21

    Could you use the Information_Schema view(s) instead?

    SELECT DISTINCT table_name, table_schema
    FROM INFORMATION_SCHEMA.TABLES
    

    According to the MSDN page (for SQL Server 2008 and above),

    Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of a object is to query the sys.objects catalog view.

    However, it seems that they're probably referring to an issue where you have a table name and are trying to find its schema, which wouldn't work if there were multiple tables with the same name (in different schemas). If you're querying for multiple results (not just trying to find the schema for a specific table), then it should be fine.

提交回复
热议问题