是否有一个SQL语句将列出MS SQL Server数据库中按模式名称排序的所有表,视图和存储过程的名称?
我想从此列表生成一个Excel电子表格,其中包含以下列:schema,type(table,view,stored proc)和name。
答案 0 :(得分:9)
以下是您的要求:
select
s.name as [Schema],
o.type_desc as [Type],
o.name as [Name]
from
sys.all_objects o
inner join sys.schemas s on s.schema_id = o.schema_id
where
o.type in ('U', 'V', 'P') -- tables, views, and stored procedures
order by
s.name
答案 1 :(得分:1)
您可以使用系统视图INFORMATION_SCHEMA.TABLES
,INFORMATION_SCHEMA.VIEWS
和INFORMATION_SCHEMA.COLUMNS
编辑:哦和INFORMATION_SCHEMA.ROUTINES
用于存储过程
答案 2 :(得分:1)
这是我最终使用的SQL语句:
SELECT
CASE so.type
WHEN 'U' THEN 'table'
WHEN 'P' THEN 'stored proc'
WHEN 'V' THEN 'view'
END AS [type],
s.name AS [schema],
so.[name] AS [name]
FROM sys.sysobjects so
JOIN sys.schemas s
ON so.uid = s.schema_id
WHERE so.type IN ('U', 'P', 'V')
ORDER BY [type], [schema], [name] asc
答案 3 :(得分:0)
从
开始select * from sys.sysobjects
编辑:现在使用架构
select * from sys.sysobjects
inner join sys.schemas on sys.sysobjects.uid = sys.schemas.schema_id
答案 4 :(得分:0)
答案 5 :(得分:0)
TRY:
SELECT
ROUTINE_SCHEMA,ROUTINE_TYPE ,ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
UNION
SELECT
TABLE_SCHEMA,TABLE_TYPE,TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
UNION
SELECT
TABLE_SCHEMA,'VIEW' ,TABLE_NAME
FROM INFORMATION_SCHEMA.VIEWS
ORDER BY ROUTINE_SCHEMA,ROUTINE_TYPE ,ROUTINE_NAME
答案 6 :(得分:0)
如果您需要深入了解列及其数据类型,请尝试使用此SQL。您可以将这些选项用于sysobjects.xtype(U =用户表,P =存储过程,V =视图)
SELECT object_type = sysobjects.xtype,
table_name = sysobjects.name,
column_name = syscolumns.name,
datatype = systypes.name,
length = syscolumns.length
FROM sysobjects
JOIN syscolumns ON sysobjects.id = syscolumns.id
JOIN systypes ON syscolumns.xtype=systypes.xtype
WHERE sysobjects.xtype='U' AND
syscolumns.name LIKE '%[column_name_here]%'
AND sysobjects.name LIKE '%[table or Stored Proc Name]%'
ORDER BY sysobjects.name,syscolumns.colid