List table names, owner, schema and columns in SQL server database

谁说胖子不能爱 提交于 2019-12-11 11:38:04

问题


In SQL SERVER how can I get a list of all table names, column names and owners?
I have done this but where do I get the OWNER details?

SELECT t.name AS tableName, 
       s.name SchemaName 
FROM   sys.tables AS t 
       INNER JOIN sys.schemas AS s 
               ON t.[schema_id] = s.[schema_id] 

回答1:


Note that "TABLE_OWNER" is that same as "SCHEMA Owner" and "TABLE_TYPE" will identify if the item is a table OR view.

Hope this helps!

--This will return all tables, table owners and table types for all database(s) that are NOT 'Offline'
--Offline database information will not appear

Declare @temp_table table(
DB_NAME varchar(max),
TABLE_OWNER varchar(max),
TABLE_NAME varchar(max),
TABLE_TYPE varchar(max),
REMARKS varchar(max)
)

INSERT INTO @temp_table (DB_NAME, TABLE_OWNER, TABLE_NAME, TABLE_TYPE,REMARKS)

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_tables'

SELECT * 
FROM @temp_table 
--Uncomment below if you are seaching for 1 database
--WHERE DB_NAME = '<Enter specific DB Name>'

--For all databases other than 'System Databases'
WHERE DB_NAME not in ('master','model','msdn','tempdb')
order by 1



回答2:


Have you tried using the built-in sp_tables stored procedure? See http://msdn.microsoft.com/en-us/library/ms186250.aspx for usage.

I would have added this as a comment, but I would apparently need 50 reputation to do so.



来源:https://stackoverflow.com/questions/25156403/list-table-names-owner-schema-and-columns-in-sql-server-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!