Query to list all stored procedures

后端 未结 23 1493
暖寄归人
暖寄归人 2020-11-28 17:18

What query can return the names of all the stored procedures in a SQL Server database

If the query could exclude system stored procedures, that would be even more he

23条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 17:56

    I've tweaked LostCajun's excellent post above to exclude system stored procedures. I also removed "Extract." from the code because I couldn't figure out what it's for and it gave me errors. The "fetch next" statement inside the loop also needed an "into" clause.

    use <>
    go
    
    declare @aQuery nvarchar(1024);
    declare @spName nvarchar(64);
    declare allSP cursor for
        select p.name  
        from sys.procedures p 
        where p.type_desc = 'SQL_STORED_PROCEDURE' 
        and LEFT(p.name,3) NOT IN ('sp_','xp_','ms_')
        order by p.name;
    open allSP;
    fetch next from allSP into @spName;
    while (@@FETCH_STATUS = 0)
    begin
        set @aQuery = 'sp_helptext [' + @spName + ']';
        exec sp_executesql @aQuery;
        fetch next from allSP into @spName;
    end;
    close allSP;
    deallocate allSP;
    

提交回复
热议问题