Get list of tables but not include system tables (

2019-04-08 23:51发布

I know I can get a list of tables from a given database with the following query:

select *
from information_schema.tables

How do I go about excluding system tables though?

5条回答
小情绪 Triste *
2楼-- · 2019-04-09 00:24

I know this is quite an old question too, but you can execute the sql server stored procedure :

EXEC sp_tables @table_name="%", @table_owner="%",@table_type="'TABLE'"

and retrieve your table list. But is not supported with SQL 2K. Exists since the 2005 server...

查看更多
相关推荐>>
3楼-- · 2019-04-09 00:27
    SELECT *
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME NOT LIKE 'sys%'

Not an elegant solution but this was my quick way to exclude dbo.sysdiagrams, which was the only system table in my list.

查看更多
成全新的幸福
4楼-- · 2019-04-09 00:28
SELECT name FROM [database].sys.tables where is_ms_shipped=0
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-04-09 00:32
select name from sysobjects where type='U'
查看更多
Rolldiameter
6楼-- · 2019-04-09 00:45

I know this is quite an old question, but someone's just edited it to resurrect it, and the "right" answer from my perspective isn't either of the two listed. The accepted answer includes some "system" tables (dtproperties is mentioned in the comments. If the user had any replication going on, they'd have found a few more).

The other answer uses a 2005 table, but is so nearly correct. For 2000, you want to use OBJECTPROPERTY instead:

select name from sysobjects where
    OBJECTPROPERTY(id,N'IsTable')=1 and
    OBJECTPROPERTY(id,N'IsMSShipped')=0
查看更多
登录 后发表回答