在 SQL 服务器中检索 Sequence 对象的定义

Retrieve definition of Sequence object in SQL Server

如何获取 SQL 服务器中任何 Sequence 对象的定义?例如,如果我想获得 View/Function/Procedure 的定义,我会使用下面的查询

SELECT OBJECT_DEFINITION(tab.OBJECT_ID) 
FROM SYS.OBJECTS tab  
WHERE tab.[type] = 'V' /*FOR VIEW*/

SELECT OBJECT_DEFINITION(tab.OBJECT_ID) 
FROM SYS.OBJECTS tab  
WHERE tab.[type] = 'P' /*FOR PROCEDURE*/

SELECT OBJECT_DEFINITION(tab.OBJECT_ID) 
FROM SYS.OBJECTS tab  
WHERE tab.[type] = 'TR' /*FOR TRIGGER*/

如果我们有类似的选项可用于获取 Sequence 个对象的详细信息,请告诉我

A​​ SEQUENCEVIEWPROCEDURE 等对象的定义类型不同,但是,您 可以 生成你自己的:

CREATE SEQUENCE dbo.YourSEQUENCE
       START WITH 7
       INCREMENT BY 4;
GO
SELECT NEXT VALUE FOR dbo.YourSEQUENCE;
GO
SELECT *
FROM sys.sequences
GO

SELECT CONCAT(N'CREATE SEQUENCE ' + QUOTENAME(s.[name]) + N',' + QUOTENAME(sq.[name]),NCHAR(13) + NCHAR(10),
              N'       START WITH ',CONVERT(int,sq.start_value), NCHAR(13) + NCHAR(10),
              N'       INCREMENT BY ',CONVERT(int,sq.increment),N';')
FROM sys.schemas s
     JOIN sys.sequences sq ON s.schema_id = sq.schema_id
WHERE s.[name] = N'dbo'
  AND sq.[name] = N'yourSEQUENCE';

GO
DROP SEQUENCE dbo.YourSEQUENCE;

如果是这样,那么您拥有所有定义的存储库,它应该已经在您的源代码管理软件中。

您的上述查询是正确的..... 即'V' -- FOR VIEW 'P' -- 程序 'TR' -- 触发器

 SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS object_name, o.type, o.type_desc, sm.definition  
    FROM sys.sql_modules AS sm  
    JOIN sys.objects AS o ON sm.object_id = o.object_id  
    ORDER BY o.type;  

使用此查询...您将获得单个集中的所有数据,只需参考类型列名称。

Objects of type P, RF, V, TR, FN, IF, TF, and R have an associated SQL module.

The SQL Server Database Engine assumes that object_id is in the current database context. 

The collation of the object definition always matches that of the calling database context.

    OBJECT_DEFINITION applies to the following object types:

    C = Check constraint

    D = Default (constraint or stand-alone)

    P = SQL stored procedure

    FN = SQL scalar function

    R = Rule

    RF = Replication filter procedure

    TR = SQL trigger (schema-scoped DML trigger, or DDL trigger at either the database or server scope)

    IF = SQL inline table-valued function

    TF = SQL table-valued function

    V = View

要获得更好的信息...请使用此 link...

https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-sql-modules-transact-sql?view=sql-server-ver15