views:

49

answers:

2

Hello everyone,

I am using SQL Server 2008. One int column I used as primary key but not identity column (whose value will be increased by 1 automatically). I want to convert this column to identity column. Any solutions?

thanks in advance, George

+1  A: 

Go to your table object in object explorer in sql server right click on a table say modify, than click on a field which is primary key that you want to convert to identity, than below you'll see column properties , there you need to change as (Is Identity) Yes and Idendity Increment 1.

Aayan
Yes it will do that but this is a bad technique to use. It does what@Quassnoi describes above but if you are going to make changes to a table they should not be done through the GUI but only through scripts that are placed in Source control. This ensures that the process is tested and run properly on all servers and mistakes are not made as well as having a history of the changes and who made them. T-SQl is code, it should be treated as such.
HLGEM
Well he didn't ask for script or he didn't mentioned about any other environment that you will need to worry. I assumed he just want to change it. For the question described above i would consider simplest way is to do it by UI.
Aayan
+5  A: 

Unfortunately, you cannot change a field to IDENTITY on an existing table.

You should:

  • Create a new table with the IDENTITY field
  • Issue SET IDENTITY_INSERT ON for the new table
  • Insert the data into the new table
  • Issue SET IDENTITY_INSERT OFF for the new table
  • Drop the old table
  • Rename the new table to the old name.

You can use SSMS to change the field type, it will do all this for you behind the scenes.

Here's a sample table:

CREATE TABLE plain (id INT NOT NULL PRIMARY KEY)

INSERT
INTO    plain
VALUES  (1000)

and the script generated by SSMS:

SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_plain
    (
    id int NOT NULL IDENTITY (1, 1)
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_plain SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_plain ON
GO
IF EXISTS(SELECT * FROM dbo.plain)
     EXEC('INSERT INTO dbo.Tmp_plain (id)
        SELECT id FROM dbo.plain WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_plain OFF
GO
DROP TABLE dbo.plain
GO
EXECUTE sp_rename N'dbo.Tmp_plain', N'plain', 'OBJECT' 
GO
ALTER TABLE dbo.plain ADD CONSTRAINT
    PK__plain__3213E83F1A609306 PRIMARY KEY CLUSTERED 
    (
    id
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
COMMIT
Quassnoi
@Quassnoi - +1 it's worth mentioning that in order to insert in the data into the new Identity column the OP will have to use `SET IDENTITY_INSERT ON|OFF`
Barry
@Barry: nice point, thanks
Quassnoi
Why I need to SET IDENTITY_INSERT OFF finally? I think I need to enable identity column on the table. I should set it ON?
George2
`@George2`: `IDENTITY_INSERT` allows inserting arbitrary values into the columns. That's not a standard behavior: normally, you should let `IDENTITY` to generate the values.
Quassnoi
And don't forget that before you do this, you will have to script out and drop any FK constraints (or it won't let you drop the table) and then put them back on when you are done.
HLGEM
Cool, question answered!
George2