Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 Express Edition - how to create a sequence

I'm using SQL Server 2008 Express Edition.

I wanna create a sequence with this code:

CREATE SEQUENCE Postoffice_seq
    AS bigint
    START WITH 1
    INCREMENT BY 1
    MINVALUE 0
    NO MAXVALUE;

and the error is

Msg 343, Level 15, State 1, Line 1
Unknown object type 'SEQUENCE' used in a CREATE, DROP, or ALTER statement.

Can anyone help me?

Best Regards!

like image 295
user437925 Avatar asked Nov 03 '11 17:11

user437925


People also ask

How do I create a sequence in SQL Server?

The syntax to create a sequence in SQL Server (Transact-SQL) is: CREATE SEQUENCE [schema.] sequence_name [ AS datatype ] [ START WITH value ] [ INCREMENT BY value ] [ MINVALUE value | NO MINVALUE ] [ MAXVALUE value | NO MAXVALUE ] [ CYCLE | NO CYCLE ] [ CACHE value | NO CACHE ]; AS datatype.

How do you create a sequence in an existing table in SQL?

Using Sequence Object with INSERT We will use sequence object to insert a value in the Id column whenever a new record is inserted into the table. Next, we will create a sequence object with an initial value of 1. We will increment this counter by 1.

How do I create a database sequence?

The syntax to create a sequence in Oracle is: CREATE SEQUENCE sequence_name MINVALUE value MAXVALUE value START WITH value INCREMENT BY value CACHE value; sequence_name. The name of the sequence that you wish to create.

How do you set a sequence number in SQL?

To number rows in a result set, you have to use an SQL window function called ROW_NUMBER() . This function assigns a sequential integer number to each result row. However, it can also be used to number records in different ways, such as by subsets.


1 Answers

SQL Server 2008 doesn't know sequences yet - that'll be introduced in SQL Server 2012 (f.k.a. "Denali").

For pretty much the same result, use an INT IDENTITY column instead:

CREATE TABLE dbo.YourTable
  (YourID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    ....
  )

The IDENTITY column is automatically filled by SQL Server at the time you insert a new row into the table. SQL Server makes sure it's monotonically increasing, starting at 1, increasing by 1 (you can set these to different values, if needed).

Basically, when inserting a row into such a table, you must not specify the IDENTITY column in your list of columns to insert values into - SQL Server will do this for you automatically.

like image 91
marc_s Avatar answered Sep 29 '22 08:09

marc_s