如何确定 SQL Server 2012 中是否存在序列?

问题描述 投票:0回答:6

我需要创建一个 SQL 脚本来确定 SQL Server 2012 数据库中是否存在序列。我熟悉确定存储过程是否存在但不存在序列的过程。谢谢。

sql sql-server sql-server-2012
6个回答
39
投票

确定 SQL Server 2012 中是否存在序列的脚本与检查存储过程非常相似。考虑以下代码来检查存储过程是否存在:

SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[SProc_Name]') AND type IN (N'P', N'PC')

类型的“P”和“PC”值指定 sys.object 的类型是 SQL 存储过程还是程序集 (CLR) 存储过程。要检查序列,您只需将其更改为“SO”,这表明它是一个序列对象:

SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Sequence_Name]') AND type = 'SO'

例如,如果您想创建一个不存在的Sequence,则可以使用以下代码:

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

我希望这有帮助!


13
投票

通过检查sys.sequences表中的数据:

select *
from sys.sequences
where object_id = object_id('schema_name.sequence_name')

实际上,如果您确定除了名称等于

'schema_name.sequence_name'
的序列之外没有其他对象,您只需检查
object_id('schema_name.sequence_name') is not null

sql 小提琴演示


10
投票

这是 Tim S 的另一个快捷版本:

IF OBJECT_ID('schema_name.sequence_name', 'SO') IS NOT NULL 
BEGIN
    DROP SEQUENCE schema_name.sequence_name
END

3
投票

试试这个。 这将列出给定数据库的所有序列。

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

2
投票

我正在使用 SQL Server 17.4(MS SQL Sever 的最新版本),并且此 SQL 脚本适用于我。 例如,此 SQL 脚本会创建序列(如果不存在)。

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;

0
投票

INFORMATION_SCHEMA.SEQUENCES 怎么样?

IF NOT EXISTS
(
    SELECT SEQUENCE_SCHEMA, SEQUENCE_NAME, DATA_TYPE
    FROM INFORMATION_SCHEMA.SEQUENCES 
    WHERE SEQUENCE_SCHEMA = 'dbo' 
    AND SEQUENCE_NAME = 'CountBy' 
)
BEGIN
    EXECUTE('CREATE SEQUENCE dbo.CountBy START WITH 1 INCREMENT BY 1; '); 
END 

如果 INFORMATION_SCHEMA.SEQUENCES 不存在,为什么不创建它?

CREATE VIEW INFORMATION_SCHEMA.SEQUENCES AS 
SELECT 
     DB_NAME() AS SEQUENCE_CATALOG 
    ,SCHEMA_NAME(s.schema_id) AS SEQUENCE_SCHEMA 
    ,s.name AS SEQUENCE_NAME 
    ,ISNULL(TYPE_NAME(s.system_type_id), t.name) AS DATA_TYPE 
    ,s.precision AS NUMERIC_PRECISION 

    ,CONVERT
    ( 
        smallint 
        ,CASE  -- int/money/decimal/numeric 
            WHEN s.system_type_id IN (48, 52, 56, 60, 106, 108, 122, 127) THEN 10 
            WHEN s.system_type_id IN (59, 62) THEN 2 
         END 
     ) AS NUMERIC_PRECISION_RADIX 
            
    ,ODBCSCALE(s.system_type_id, s.scale) AS NUMERIC_SCALE -- for real/float
     
    ,s.start_value AS START_VALUE
    ,s.minimum_value AS MINIMUM_VALUE
    ,s.maximum_value AS MAXIMUM_VALUE
    ,s.increment AS INCREMENT
    ,s.is_cycling AS CYCLE_OPTION
    ,t.name AS DECLARED_DATA_TYPE
    ,t.precision AS DECLARED_NUMERIC_PRECISION
    ,t.scale AS DECLARED_NUMERIC_SCALE
FROM sys.sequences AS s
INNER JOIN sys.types t ON s.user_type_id = t.user_type_id 
© www.soinside.com 2019 - 2024. All rights reserved.