Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Exclude System Table When Querying Sys.Tables?

I'm running this 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

And the result is:

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

sysdiagrams is a system table but showed in result.

Update: Thanks all for your answers and comments, I'm using Nate Bolam & vmvadivel answers:

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'))
like image 275
Coding Junkies Avatar asked Jan 08 '12 02:01

Coding Junkies


1 Answers

SSMS uses an extended property to mark the sysdiagrams table as a sort of pseudo system table.

Try this:

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'))
like image 134
Nate Bolam Avatar answered Sep 28 '22 06:09

Nate Bolam