get the text of a stored procedure into a variable in SQL Server

前端 未结 3 1315
天命终不由人
天命终不由人 2021-01-18 07:10

I want to loop through several stored procs and extract one string form each of them to use in another procedure (basically the 4-part remote server string)

So I can

3条回答
  •  情歌与酒
    2021-01-18 07:23

    You can use the system stored procedure sp_helptext to get the stored procedure definition into a table and then just query against that temp table / table variable something like this..

    DECLARE @TABLE TABLE
    (Line_Number INT IDENTITY(1,1), Text_Column NVARCHAR(MAX));
    
    INSERT INTO @TABLE(Text_Column)
    EXECUTE sp_helptext Your_Proc_Name
    
    SELECT * 
    FROM @TABLE
    WHERE Text_Column LIKE '%BOCTEST%'
    

    UPDATE

    SELECT p.name
          ,m.[definition]
    FROM  sys.procedures p 
    INNER JOIN sys.sql_modules m
    ON    P.[object_id] = m.[object_id]
    WHERE m.[definition] LIKE '%BOCTEST%'
    

提交回复
热议问题