SQL常用语句

来源:互联网 发布:c 网络爬虫 编辑:程序博客网 时间:2024/05/20 14:43
--sql查询某个表被那些存储过程用到
select distinct object_name(id) from syscomments
where id in (select object_id from sys.objects where type ='P')
and  UPPER(text) like '%READ_IVTEST%' order by object_name(id)
/*type char(2) 对象类型。可以是下列值之一:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
FN = 标量函数
IF = 内嵌表函数
K = PRIMARY KEY 或 UNIQUE 约束
L = 日志
P = 存储过程
R = 规则
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
V = 视图
X = 扩展存储过程*/

---查看锁表--spid 锁表进程--tableName 被锁表名
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT'

---解锁语句
 declare @spid int
 Set @spid = 136
 --锁表进程
 declare @sql varchar(1000)set
 @sql='kill '+cast(@spid as varchar)exec(@sql)


 --查询所有的存储过程
 select a.name,a.[type],b.[definition] from sys.all_objects a,sys.sql_modules b
where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P')
order by a.[name] asc