Can't create sequence in SQL Server 2008

前端 未结 3 2016
感动是毒
感动是毒 2020-12-21 00:08

I have tried to create a sequence in SQL Server 2008 using the following query,

CREATE SEQUENCE serial START 100

I got the following syntax

相关标签:
3条回答
  • 2020-12-21 00:15

    You just cannot do this.

    SQL Server 2008 does not have the concept of a SEQUENCE - this is a new feature in SQL Server 2012 (MSDN documentation here).

    Typically, you want to create an "auto-increasing" column for your table - in that case, use the IDENTITY column attribute instead:

    CREATE TABLE dbo.YourTable
    ( TableID INT IDENTITY,
     .....
    
    0 讨论(0)
  • 2020-12-21 00:20

    Make use of IDENTITY column will do your task.

    Get full details on msdn : IDENTITY

    0 讨论(0)
  • 2020-12-21 00:31

    How marc_s has wrote, SQL Server 2008 does not have sequences yet. But we can use some trick to imitate sequences behaviour. Sometimes we need generate identifier for some table without inserting any data. Or generate IDs for different tables. In such situation I use this structure.

    Create a table:

    create table IdSequence (id int identity(1,1))
    

    Then I use this script to generate Id:

    begin tran 
     insert into IdSequence output inserted.id default values 
    rollback tran
    

    In my app it looks like:

        public Int32 GetNextId()
        {
            Int32 id = 0;
            using (var transaction = Session.BeginTransaction())
            {
                id = Session.CreateSQLQuery("insert IdSequence output inserted.id default values").UniqueResult<Int32>();
                transaction.Rollback();
            }
    
            return id;
        }
    
    0 讨论(0)
提交回复
热议问题