【转】SQL Server查询数据库所有存储过程、触发器、索引信息SQL分享

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 Parameter
from sys.procedures Pr left join
sys.parameters parameter on Pr.object_id = parameter.object_id
inner join sys.types Type on parameter.system_type_id = Type.system_type_id
where 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 Parameter
from sys.procedures Pr left join
sys.parameters parameter on Pr.object_id = parameter.object_id
inner join sys.types Type on parameter.system_type_id = Type.system_type_id
where type = ‘P‘
)tb
where Pr_Name not like ‘sp_%‘ --and Pr_Name not like ‘dt%‘
group by Pr_Name
order 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 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 null
end as [触发器类型描述]
from sys.triggers triggers
inner join sys.tables tables on triggers.parent_id = tables.object_id
where triggers.type =‘TR‘
order by triggers.create_date

4. 查询所有索引

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 ind
inner 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_id
inner 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 ind
inner 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_id
inner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id
)tb
where Tab_Name not like ‘sys%‘
group by Tab_Name,Index_Name
) indexs inner join sys.indexes Ind_Attribute on indexs.Index_Name = Ind_Attribute.name
order by indexs.Tab_Name

5. 显示存储过程内容

SELECT TEXT FROM syscomments WHERE id=object_id(‘SP_NAME‘)

SP_HELPTEXT ‘SP_NAME‘

原文地址:https://www.cnblogs.com/taofengfeng/p/10898281.html

时间: 2024-10-31 06:58:50

【转】SQL Server查询数据库所有存储过程、触发器、索引信息SQL分享的相关文章

sql server 查询数据库所有的表名+字段

原文:sql server 查询数据库所有的表名+字段 SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT    (case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空       a.colorder as 字段序号,       a.name as 字段名,       (case when COLUMNPROPER

SQL Server —— 查询数据库、表、列等

一.查询数据库(sys.databases -- select *from sys.databases where name='<数据库名>') select *from sys.databases where name='MyDatabase' 二.查询表(sysobjects -- select *from sysobjects where id=OBJECT_ID('<表名>')) select *from sysobjects where id=OBJECT_ID('Stu

SQL Server查询数据库空间分配情况、数据库备份信息

查询数据库空间分配情况: 1 use master 2 go 3 create procedure dbo.proc_getdbspaceused 4 as 5 begin 6 set nocount on 7 create table #dbsize( 8 database_id int 9 ,database_name nvarchar(1024) 10 ,size_kb bigint 11 ,space_available_kb bigint 12 ,reserved_kb bigint

批量删除Sql Server对象(表,存储过程,触发器)

先在系统表中找到要处理的表名或者是存储过程的名字,在用游标对其进行处理 PS:SqlServer 2000使用的是系统表是sysobjects,类型字段是:xtype; SqlServer 2005以上版本的系统表是Sys.Objects,类型字段是Type 本文中以Sql2005为例,Sql2000版本请自行按照上述说明进行替换 注意  sys.objects 中type的值不同 删除命令是不同的 如删除存储过程用drop PROCEDURE PROCEDURENAME 删除表用 drop t

SQL Server查询数据库近期执行的SQL语句

SELECT TOP 1000        ST.text AS '执行的SQL语句',       QS.execution_count AS '执行次数',       QS.total_elapsed_time AS '耗时',       QS.total_logical_reads AS '逻辑读取次数',       QS.total_logical_writes AS '逻辑写入次数',       QS.total_physical_reads AS '物理读取次数',    

SQL Server 查询数据库表的列数

1 select count(*) from sysobjects a 2 join syscolumns b 3 on a.id=b.id 4 where a.name='表名' 5 go

sql server查询数据库总数据条数

1: select sum(c.row_count) as datacount  from    sys.indexes a ,          sys.objects b ,          sys.dm_db_partition_stats c  where   a.[object_id] = b.[object_id]          AND b.[object_id] = c.[object_id]          AND a.index_id = c.index_id     

SQL Server查询数据库表字段类型

select b.name,a.name,c.name,a.xprec,a.xscalefrom  syscolumns aleft outer join sysobjects b ON a.id=b.id left outer join systypes c ON c.xusertype=a.xusertype where b.xtype='U' AND b.name LIKE '%CRM_PS%' AND (select systypes.name+'('+cast(a.length/2 

SQL SERVER 2005 获取表的所有索引信息以及删除和新建语句

BEGIN        WITH tx AS        (                SELECT a.object_id                      ,b.name AS schema_name                      ,a.name AS table_name                      ,c.name as ix_name                      ,c.is_unique AS ix_unique