How to create sequence in SQL Server 2008

后端 未结 6 1352
执笔经年
执笔经年 2021-01-02 23:37

I am creating sequence in SQL Server with the following code. But it displays error as unknown object type. Please give a solution

Here\'s my code :

         


        
相关标签:
6条回答
  • 2021-01-03 00:05

    We can't use Sequence easily in SQL Server 2008.

    You can use CTE(Common Table Expressions) for Sequence Generation in SQL Server 2008

    WITH NUM_GEN (n) AS
         ( 
                SELECT 1 
                UNION 
                      ALLSELECT n+1 
                FROM  NUM_GEN 
                WHERE n+1< MAX_VALUE 
         ) 
    SELECT n 
    FROM   NUM_GEN
    
    0 讨论(0)
  • 2021-01-03 00:13

    Create a Numbers table; here's a SO question on the subject. Let's call it dbo.Number.

    Have a table with an identity column. Set the seed and step to whatever is appropriate:

    create table dbo.SequenceGenerator(ID int identity(1, 1), dummy int);
    

    Then insert values from the numbers table and capture the newly-generated identity values:

    declare @HowMany int = 3;  -- This determines how large a sequence you receive
                               -- at each itteration
    declare @NewSequenceValue table (ID int);
    
    insert dbo.SequenceGenerator(dummy)
    output INSERTED.ID 
        into @NewSequenceValue
    select Number from dbo.Numbers
    where Number <= @HowMany;
    
    select * from @NewSequenceValue;
    

    Be sure to DELETE .. dbo.SequenceGenerator from time to time, else it will get big for no additional value. Do not TRUNCATE it - that will reset the IDENTITY column to its initally-declared seed value.

    0 讨论(0)
  • 2021-01-03 00:16

    Are you sure you're running 2012? I had no trouble with:

    CREATE SEQUENCE seqval
    START WITH 100
    INCREMENT BY 1 
    minvalue 100 maxvalue 10000 NO CYCLE
    

    Your 0,0 values generated a syntax error for me but a clear and simple one.

    The minimum value for sequence object 'seqval' must be less than its maximum value.
    
    0 讨论(0)
  • 2021-01-03 00:19

    SQL Server 2008 can't create sequences, Sequence objects apply to SQL Server 2012 through current versions.

    https://msdn.microsoft.com/es-es/library/ff878091(v=sql.120).aspx

    You can use an IDENTITY in your table instead, for example:

    CREATE TABLE Person(
        Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
        Name varchar(255) NOT NULL
    );
    

    The starting value for IDENTITY is 1, and it will increment by 1 for each new record.

    http://www.w3schools.com/sql/sql_autoincrement.asp

    0 讨论(0)
  • 2021-01-03 00:28

    You can do this.

    --Create a dummy TABLE to generate a SEQUENCE. No actual records will be stored.
    CREATE TABLE SequenceTABLE
    (
        ID BIGINT IDENTITY  
    );
    GO
    
    --This procedure is for convenience in retrieving a sequence.
    CREATE PROCEDURE dbo.GetSEQUENCE ( @value BIGINT OUTPUT)
    AS
        --Act like we are INSERTing a row to increment the IDENTITY
        BEGIN TRANSACTION;
        INSERT SequenceTABLE WITH (TABLOCKX) DEFAULT VALUES;
        ROLLBACK TRANSACTION;
        --Return the latest IDENTITY value.
        SELECT @value = SCOPE_IDENTITY();
    GO
    
    --Example execution
    DECLARE @value BIGINT;
    EXECUTE dbo.GetSEQUENCE @value OUTPUT;
    SELECT @value AS [@value];
    GO
    
    0 讨论(0)
  • 2021-01-03 00:29
    WITH N0 as (SELECT 1 as n UNION ALL SELECT 1)
    ,N1 as (SELECT 1 as n FROM N0 t1, N0 t2)
    ,N2 as (SELECT 1 as n FROM N1 t1, N1 t2)
    ,N3 as (SELECT 1 as n FROM N2 t1, N2 t2)
    ,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N3)
    SELECT * FROM nums
    
    0 讨论(0)
提交回复
热议问题