Saturday, July 27, 2019

How to find stored procedure containing example SQL SERVER

I was running Top Queries Consuming CPU report the top query was


"SELECT "Tbl1002"."Model" "Col1007" FROM "eTrace"."dbo"."T_WIPHeader" "Tbl1002" WITH (NOLOCK) WHERE CONVERT(nvarchar(100),"Tbl1002"."IntSN",0)=@P"

I first used the actual text in the like of the ROUTINE_DEFINITION so at first it looked like this.

SELECT
ROUTINE_NAME,
ROUTINE_DEFINITION ,
ROUTINE_SCHEMA
FROM
    INFORMATION_SCHEMA.ROUTINES
WHERE
    ROUTINE_TYPE='PROCEDURE' AND
    ROUTINE_DEFINITION LIKE '%SELECT "Tbl1002"."Model" "Col1007" FROM "eTrace"."dbo"."T_WIPHeader" "Tbl1002" WITH (NOLOCK) WHERE CONVERT(nvarchar(100),"Tbl1002"."IntSN",0)=@P%'

I was getting no results and was getting frustrated. After a short nap, I had an idea of breaking down the query something like below.

SELECT
ROUTINE_NAME,
ROUTINE_DEFINITION ,
ROUTINE_SCHEMA
FROM
    INFORMATION_SCHEMA.ROUTINES
WHERE
    ROUTINE_TYPE='PROCEDURE' AND
    ROUTINE_DEFINITION LIKE '%nvarchar(100)%' AND
    ROUTINE_DEFINITION like '%T_WIPHeader%'   AND
    ROUTINE_DEFINITION like '%IntSN%' and ROUTINE_DEFINITION like '%convert%'

From there I found out that the stored proc is called sp_WIPOutATE

then I found the line that it was creating it. It was using a dynamic SQL

select @Wipid=Wipid ,@changedon =changedon,@DJ=DJ,@Model=upper(Model),@PCBA=upper(PCBA) ,@currentprocess=upper(CurrentProcess), @wipresult=result from T_wipheader with (nolock) where IntSN=@IntSerialNo

No comments:

Post a Comment