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
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();
transaction.Rollback();
}
return id;
}