Query to list all stored procedures

后端 未结 23 1279
暖寄归人
暖寄归人 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:55

    Select list of stored procedure in SQL server. Refer here for more: https://coderrooms.blogspot.com/2017/06/select-list-of-stored-procedure-in-sql.html

    0 讨论(0)
  • 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 <<databasename>>
    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;
    
    0 讨论(0)
  • 2020-11-28 17:57

    Just the names:

    SELECT SPECIFIC_NAME  
    FROM YOUR_DB_NAME.information_schema.routines  
    WHERE routine_type = 'PROCEDURE'
    
    0 讨论(0)
  • 2020-11-28 17:59

    I wrote this simple tsql to list the text of all stored procedures. Be sure to substitute your database name in field.

    use << database name >>
    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' order by p.name;
    open allSP;
    fetch next from allSP into @spName;
    while (@@FETCH_STATUS = 0)
    begin
        set @aQuery = 'sp_helptext [Extract.' + @spName + ']';
        exec sp_executesql @aQuery;
        fetch next from allSP;
    end;
    close allSP;
    deallocate allSP;
    
    0 讨论(0)
  • 2020-11-28 18:00

    You can use one of the below queries to find the list of Stored Procedures in one database :

    Query1 :

        SELECT 
            *
        FROM sys.procedures;
    

    Query2 :

        SELECT 
            * 
        FROM information_schema.routines 
        WHERE ROUTINE_TYPE = 'PROCEDURE' 
    

    If you want to find the list of all SPs in all Databases you can use the below query :

        CREATE TABLE #ListOfSPs 
        (
            DBName varchar(100), 
            [OBJECT_ID] INT,
            SPName varchar(100)
        )
    
        EXEC sp_msforeachdb 'USE [?]; INSERT INTO #ListOfSPs Select ''?'', Object_Id, Name FROM sys.procedures'
    
        SELECT 
            * 
        FROM #ListOfSPs
    
    0 讨论(0)
  • 2020-11-28 18:00

    the best way to get objects is use sys.sql_modules. you can find every thing that you want from this table and join this table with other table to get more information by object_id

    SELECT o. object_id,o.name AS name,o.type_desc,m.definition,schemas.name scheamaName
    FROM sys.sql_modules        m 
        INNER JOIN sys.objects  o ON m.object_id=o.OBJECT_ID
        INNER JOIN sys.schemas ON schemas.schema_id = o.schema_id
        WHERE [TYPE]='p'
    
    0 讨论(0)
提交回复
热议问题