• 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;
  • 相关阅读:
    【Swin Transformer原理和源码解析】Hierarchical Vision Transformer using Shifted Windows
    【微信小程序】博客小程序,静态版本(二)引入 lin-ui 组件、设计和开发文章页
    ArcGIS综合制图教程,简单上手!
    国内网站用香港服务器会被封吗?
    医学图像处理-安装nibabel包命令
    1、手把手教你学会使用 FlinkSQL客户端
    Shading-JDBC、ShadingSphere、ShardingProxy 使用详解
    ASP.NET DataBinder.Eval与 Eval
    基于ssm的线上旅行信息管理系统(有报告)。Javaee项目,ssm项目。
    Vue / Vue CLI / Vue Router / Vuex / Element UI
  • 原文地址:https://blog.csdn.net/caiyiii/article/details/133960281