I need to create a SQL script to determine if a sequence exists in a SQL Server 2012 database. I am familiar with process for determine if a stored procedure exist but not sequence. Thank you.
5 Answers
The script to determine whether or not a Sequence exists in SQL Server 2012 is very similar to checking for Stored Procedures. Consider the following code that checks to see if a Stored Procedure exists:
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[SProc_Name]') AND type IN (N'P', N'PC')
The values of 'P' and 'PC' for the type specify the type of the sys.object is a SQL Stored Procedure or a Assembly (CLR) stored-procedure. To check for a sequence, you just need to change it to 'SO' which indicates it is a Sequence Object:
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Sequence_Name]') AND type = 'SO'
For example, if you want to create a Sequence if it doesn't exist, you could use the following code:
IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Sequence_Name]') AND type = 'SO')
CREATE SEQUENCE [dbo].[Sequence_Name]
AS [bigint]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 3
GO
I hope this helps!
-
6As of SQL Server 2005, it's preferable to use the **more focused** catalog views, like `sys.sequences` rather than querying the "catch-all" `sys.objects` and having to remember the necessary `type` for your object in question .... – marc_s Sep 26 '13 at 19:02
-
@marc_s is correct that it's easier to remember the "more focused catalog views." Out of habit, I still use [sys.objects type](https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-objects-transact-sql?view=sql-server-2017 "link to full list of types"), but I am now considering making the transition. – datalifenyc Aug 08 '19 at 14:23
-
@StevenFerrer Based on the article you provided, "Approach 4" indicates that "`sys.sysobjects`...will be deprecated" and not `sys.objects`: "Microsoft is suggesting to use the catalog view `sys.objects` instead of `sys.sysobjects` system..." – datalifenyc Aug 08 '19 at 14:28
-
1@myidealab, good catch! I've deleted my comment since it doesn't really add value to this answer. haha. thanks very much! – stevenferrer Aug 27 '19 at 01:06
By checking data in sys.sequences table:
select *
from sys.sequences
where object_id = object_id('schema_name.sequence_name')
actually that if you're sure that there's no object other than sequence with name equals 'schema_name.sequence_name'
, you could just check object_id('schema_name.sequence_name') is not null

- 107,110
- 28
- 195
- 197
Try this. This will list all of the sequences for a given database.
SELECT
seq.name AS [Sequence Name],
seq.object_id AS [Object ID],
seq.create_date AS [Creation Date],
seq.modify_date AS [Last Modified Date],
SCHEMA_NAME(seq.schema_id) AS [Schema],
CAST(seq.precision AS int) AS [Numeric Precision],
CAST(seq.scale AS int) AS [Numeric Scale],
ISNULL(seq.start_value,N'''') AS [Start Value],
ISNULL(seq.increment,N'''') AS [Increment Value],
ISNULL(seq.minimum_value,N'''') AS [Min Value],
ISNULL(seq.maximum_value,N'''') AS [Max Value],
CAST(seq.is_cycling AS bit) AS [Is Cycle Enabled],
ISNULL(seq.cache_size,0) AS [Cache Size],
ISNULL(seq.current_value,N'''') AS [Current Value]
FROM sys.sequences AS seq

- 847
- 2
- 14
- 32
I'm using SQL Server 17.4 (Latest version of MS SQL Sever) and this SQL script worked with me. This SQL script, for example, creates the sequence if not exists.
IF NOT EXISTS
(
SELECT [name]
FROM sys.sequences
WHERE [name] = 'seq_businessTripRequest'
)
BEGIN
CREATE SEQUENCE [dbo].[seq_businessTripRequest]
AS [BIGINT]
START WITH 1
INCREMENT BY 1
MINVALUE-9223372036854775808
MAXVALUE 9223372036854775807
CACHE;
END;

- 3,688
- 2
- 27
- 25