• sqlserver列出所有的存储过程


    This article presents two ways to return a list of stored procedures in a SQL Server database.

    Option 1 – The ROUTINES Information Schema View

    You can use the ROUTINES information schema view to get a list of all user-defined stored procedures in a database.

    USE Music;
    SELECT 
      ROUTINE_SCHEMA,
      ROUTINE_NAME
    FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_TYPE = 'PROCEDURE';
    

    Result:

    +------------------+----------------------+
    | ROUTINE_SCHEMA   | ROUTINE_NAME         |
    |------------------+----------------------|
    | dbo              | spAlbumsFromArtist   |
    | dbo              | uspGetAlbumsByArtist |
    +------------------+----------------------+
    

    Return The Procedure’s Definition

    The INFORMATION_SCHEMA.ROUTINES view also has a ROUTINE_DEFINITION column, so you can easily return each stored procedure’s definition if required.

    SELECT ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_TYPE = 'PROCEDURE';
    

    Option 2 – The sys.objects System Catalog View

    Another way to return a list of stored procedures is to query the sys.objects system catalog view.

    SELECT 
      SCHEMA_NAME(schema_id) AS [Schema],
      name
    FROM sys.objects
    WHERE type = 'P';
    

    Result:

    +----------+----------------------+
    | Schema   | name                 |
    |----------+----------------------|
    | dbo      | spAlbumsFromArtist   |
    | dbo      | uspGetAlbumsByArtist |
    +----------+----------------------+
    

    The type P is presumable for “Procedure”.

    Another way to do this is filter by the type_desc column:

    SELECT 
      SCHEMA_NAME(schema_id) AS [Schema],
      name
    FROM sys.objects
    WHERE type_desc = 'SQL_STORED_PROCEDURE';
    

    Return The Procedure’s Definition

    The sys.objects view doesn’t include a column for the object’s definition. If you want to return each stored procedure’s definition, you can join it with the sys.sql_modules system view.

    Example:

    SELECT definition
    FROM sys.objects o
    INNER JOIN sys.sql_modules m 
    ON o.object_id = m.object_id
    WHERE type = 'P';
    

    Option 3 – The sys.procedures Catalog View

    The sys.procedures catalog stored procedure contains a row for each object that is a procedure of some kind, with sys.objects.type = P, X, RF, and PC.

    Executing the following code will return all stored procedures that the user either owns or on which the user has been granted some permission.

    SELECT 
      SCHEMA_NAME(schema_id) AS [Schema],
      Name
    FROM sys.procedures;
    

    Result:

    +----------+----------------------+
    | Schema   | Name                 |
    |----------+----------------------|
    | dbo      | spAlbumsFromArtist   |
    | dbo      | uspGetAlbumsByArtist |
    +----------+----------------------+
    

    This view inherits the type column from sys.objects so you can filter the results by procedure type if you wish.

    SELECT 
      SCHEMA_NAME(schema_id),
      name
    FROM sys.procedures
    WHERE type = 'P';
    

    In my case, I get the same result because both of my procedures are of type “P”.

    In case you’re wondering, here’s what each type means.

    P

    SQL Stored Procedure

    X

    Extended stored procedure

    RF

    Replication-filter-procedure

    PC

    Assembly (CLR) stored-procedure

    Return The Procedure’s Definition

    The sys.procedures view doesn’t include a column for the object’s definition. As with the previous method, if you want to return each stored procedure’s definition, you can join it with the sys.sql_modules system view.

    Example:

    SELECT definition
    FROM sys.procedures p
    INNER JOIN sys.sql_modules m 
    ON p.object_id = m.object_id;
  • 相关阅读:
    【ES6】CommonJS模块和ES6模块
    games101 作业2
    基于Sider-chatgpt3.5-编写一个使用springboot2.5连接elasticsearch7的demo程序,包括基本的功能,用模板方法
    Ceres库中参数理解
    Java中long(Long)与int(Integer)之间的转换
    SpringBoot3使用Swagger
    上机实验一 顺序表的基本操作和简单程序 西安石油大学数据结构
    Linux内核虚拟化技术KVM总结以及Docker容器技术的浅析(以x86架构为例)
    C++QT实现压缩文件、文件夹和解压缩操作
    触发器的类型有哪些?
  • 原文地址:https://blog.csdn.net/caiyiii/article/details/133960281