views:

1914

answers:

3

I need to have one column as the primary key and another to auto increment an order number field. Is this possible?

EDIT: I think I'll just use a composite number as the order number. Thanks anyways.

A: 

in sql server it's not possible to have more than one column as identity.

Mladen Prajdic
+1  A: 

The primary key doesn't need to be an identity column.

You can't have two Identity columns.

You could get something close to what you want with a trigger...

Joe R
+2  A: 
CREATE TABLE [dbo].[Foo](
 [FooId] [int] IDENTITY(1,1) NOT NULL,
 [BarId] [int] IDENTITY(1,1) NOT NULL
)

returns

Msg 2744, Level 16, State 2, Line 1
Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed.

So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity).

Edit: msdn:CREATE TABLE (Transact-SQL) and CREATE TABLE (SQL Server 2000):

Only one identity column can be created per table.

eed3si9n
I tried this but often a setting can be changed which is why I asked the question.
William
It's by spec. See the added link to msdn.
eed3si9n