user1941025
user1941025

Reputation: 611

Retrieve definition of Sequence object in SQL Server

How can I get the definition of any Sequence objects in SQL Server? For instance if I want to get the definition of View/Function/Procedure I would use below query

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*/

Please let me know if we have similar options available to get the details for Sequence objects

Upvotes: 3

Views: 1971

Answers (2)

THE LIFE-TIME LEARNER
THE LIFE-TIME LEARNER

Reputation: 1522

Your Above Query is right..... i.e.'V' -- FOR VIEW 'P' -- FOR PROCEDURE 'TR' -- FOR TRIGGER

 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;  

Use this Query...you will get all the data in single set just refer type Column Name.

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

For better info ...use this link...

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

Upvotes: 1

Thom A
Thom A

Reputation: 95749

A SEQUENCE doesn't have the same type of definition as an object like a VIEW or PROCEDURE, however, you could generate your own:

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;

If this is so you have a repository of all your definitions, that should already be in your Source Control Software.

Upvotes: 3

Related Questions