由于在公司服务器上在某个时段查询某个sql执行比较慢,然后查到了某个sql里面含有in,造成数据库服务器经常超时,又不想一条一条的看,然后写了一个方法
CREATE Procedure GetKey @Key Varchar(200)=‘‘,@Type VarChar(10)=‘and‘
As
Set NoCount On
Declare @S Varchar(400),@If Varchar(400)
Set @S=‘Select ‘‘SPText ‘‘ as SH,Object_Name(S.[Object_ID]) as Obj,O.Modify_Date‘+Char(13)
Set @[email protected]+‘ From Sys.SQL_Modules S Join Sys.Objects O On S.Object_ID=O.Object_ID‘+Char(13)
Set @If=‘‘
While CharIndex(‘..‘,@Key)>0
Begin
Set @[email protected]+‘ ‘[email protected]+‘ S.[Definition] Like ‘‘%‘+SubString(@Key,1,CharIndex(‘..‘,@Key)-1)+‘%‘‘‘+Char(13)
Set @Key=SubString(@Key,CharIndex(‘..‘,@Key)+2,80)
End
If @Key>‘‘ Set @[email protected]+‘ ‘[email protected]+‘ S.[Definition] Like ‘‘%‘[email protected]+‘%‘‘‘+Char(13)
If @If>‘‘ Set @If=‘ Where ‘+SubString(@If,Case When @Type=‘and‘ Then 5 Else 4 End,400)
Exec(@[email protected]+‘ Order By 3 Desc‘)