sqlserver查询数据库所有存储过程、触发器、索引信息

来源:互联网 发布:淘宝男士服装店铺推荐 编辑:程序博客网 时间:2024/04/29 19:37

1. 查询所有存储过程

select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter]from (select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameterfrom sys.procedures Pr left joinsys.parameters parameter on Pr.object_id = parameter.object_idinner join sys.types Type on parameter.system_type_id = Type.system_type_idwhere type = 'P') t where Pr_Name=tb.Pr_Name for xml path('')), 1, 1, '')from (select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameterfrom sys.procedures Pr left joinsys.parameters parameter on Pr.object_id = parameter.object_idinner join sys.types Type on parameter.system_type_id = Type.system_type_idwhere type = 'P')tbwhere Pr_Name not like 'sp_%' --and Pr_Name not like 'dt%'group by Pr_Nameorder by Pr_Name

2. 存储过程信息查询

 select Pr.Name as Pr_Name,parameter.name,T.Name,convert(varchar(32),parameter.max_length) as 参数长度,parameter.is_output as 是否是输出参数,parameter.* from sys.procedures Pr left join sys.parameters parameter on Pr.object_id = parameter.object_id inner join sys.types T on parameter.system_type_id = T.system_type_id where Pr.type = 'P' and Pr.Name like 'order_%' and T.name!='sysname' order by Pr.Name


3. 显示存储过程内容

SELECT TEXT FROM syscomments WHERE id=object_id('SP_NAME')SP_HELPTEXT 'SP_NAME'

4. 查询所有触发器

select triggers.name as [触发器],tables.name as [表名],triggers.is_disabled as [是否禁用],triggers.is_instead_of_trigger AS [触发器类型],case when triggers.is_instead_of_trigger = 1 then 'INSTEAD OF'when triggers.is_instead_of_trigger = 0 then 'AFTER'else nullend as [触发器类型描述]from sys.triggers triggersinner join sys.tables tables on triggers.parent_id = tables.object_idwhere triggers.type ='TR'order by triggers.create_date

5. 查询所有索引

select indexs.Tab_Name as [表名],indexs.Index_Name as [索引名] ,indexs.[Co_Names] as [索引列],Ind_Attribute.is_primary_key as [是否主键],Ind_Attribute.is_unique AS [是否唯一键],Ind_Attribute.is_disabled AS [是否禁用]from (select Tab_Name,Index_Name, [Co_Names]=stuff((select ','+[Co_Name] from( select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes indinner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_idinner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id) t where Tab_Name=tb.Tab_Name and Index_Name=tb.Index_Name for xml path('')), 1, 1, '')from (select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes indinner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_idinner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id)tbwhere Tab_Name not like 'sys%'group by Tab_Name,Index_Name) indexs inner join sys.indexes Ind_Attribute on indexs.Index_Name = Ind_Attribute.nameorder by indexs.Tab_Name

阅读全文
0 0
原创粉丝点击