简体   繁体   中英

Get the names of all the tables and the views in a single SQL query for SQL Server?

I know that INFORMATION_SCHEMA.TABLES gives the table names and sys.views gives the view names. But I'm not able to get both in a single query result. Any help is appreciated.

sys.views ( sys.views (Transact-SQL) ) doesn't give you the list of column names, it (unsurprisingly) gives you the list VIEW objects in the database:

Contains a row for each view object, with sys.objects.type = V.

You either want the (appropriately named) INFORMATION_SCHEMA.COLUMNS or perform a JOIN from sys.tables to sys.columns on object_id .


That one word you changed completely changes the question. Instead you now want sys.objects :

SELECT [name]
FROM sys.objects o
WHERE [type] IN ('U','V');

'U' means User Table, and 'V' means View.

Your understanding of INFORMATION_SCHEMA.TABLES is incorrect. It has both tables and views -- despite the name.

If you want one or the other type, then check TABLE_TYPE which can be either 'VIEW' or 'BASE_TABLE' .

So this does what you want:

select t.*
from information_schema.tables t;

You can try this SQL:

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='dbName'

Below system procedure will get all tables and views names. Read more on sp_tables

sp_tables

You can also filter the list using wildcharacters.

sp_tables 'tablename%'

Additional Info: There is a corresponding procedure for stored procedures list sp_stored_procedures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM