查询Sys.Tables时如何排除系统表?

时间:2012-01-08 02:30:22

标签: sql-server sql-server-2008

我正在运行这个SQL:

SELECT S.name as Owner, T.name as TableName FROM  sys.tables AS T
JOIN sys.schemas AS S ON S.schema_id = T.schema_id

结果是:

Owner   TableName
------------------------
dbo         Person
dbo         Customer
dbo         sysdiagrams

sysdiagramssystem table,但在结果中显示。

更新 感谢大家的回答和评论,我正在使用 Nate Bolam & vmvadivel 答案:

SELECT S.name as Owner, T.name as TableName 
FROM  
  sys.tables AS T
    INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id
    LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
WHERE 
  T.is_ms_shipped = 0 AND 
  (EP.class_desc IS NULL OR (EP.class_desc <>'OBJECT_OR_COLUMN' AND 
  EP.[name] <> 'microsoft_database_tools_support'))

3 个答案:

答案 0 :(得分:9)

SSMS使用扩展属性将sysdiagrams表标记为一种伪系统表。

试试这个:

SELECT S.name as Owner, T.name as TableName FROM  sys.tables AS T
INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
WHERE (EP.class_desc IS NULL 
OR (EP.class_desc <> 'OBJECT_OR_COLUMN'
    AND EP.[name] <> 'microsoft_database_tools_support'))

答案 1 :(得分:3)

您可以使用IS_MS_SHIPPED。希望以下脚本是您正在寻找的

SELECT 
    S.[name] AS Owner, 
    T.[name] AS TableName 
FROM  
    sys.tables AS T JOIN sys.schemas AS S 
    ON S.schema_id = T.schema_id
WHERE 
    T.is_ms_shipped = 0 
    AND T.[name] <> 'sysdiagrams'

答案 2 :(得分:1)

请您尝试使用以下查询,谢谢

SELECT S.name as Owner, T.name as TableName FROM  sys.tables AS T
JOIN sys.schemas AS S ON S.schema_id = T.schema_id
WHERE T.name <> 'sysdiagrams'