Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Change Identity Property in T-SQL


Change Identity Property in T-SQL

Author
Message
Welsh Corgi
Welsh Corgi
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5164 Visits: 4869
How do you change the Identity Property in T-SQL?

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
free_mascot
free_mascot
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2879 Visits: 2235
You can use ALTER TABLE command.

---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
What do you mean?

Change the column that is an IDENTITY?
Add or remove an IDENTITY column?
Change the seed?



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Welsh Corgi
Welsh Corgi
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5164 Visits: 4869
Sean Pearce (5/20/2014)
What do you mean?

Change the column that is an IDENTITY?
Add or remove an IDENTITY column?
Change the seed?



There is an Identity Property for a column it is set to True or False.

You can't change it from falsetto true.

You can change it in design mode from True to false but how do you do this in T-SQL Code.

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
You can't turn an existing IDENTITY off and you can't make an existing column IDENTITY. However you can add an IDENTITY column to an existing table.

Any indexes should be scripted, dropped and recreated after the change.
-- Setup test table
CREATE TABLE TestIdentity
(ID INT IDENTITY(1, 1),
Value INT);
GO

-- Setup test data
INSERT INTO TestIdentity (Value)
VALUES (1),(2),(3),(4),(5);
GO

-- Add new column
ALTER TABLE TestIdentity
ADD ID2 INT NULL;
GO

-- Copy data
UPDATE TestIdentity
SET ID2 = ID;
GO

-- Remove IDENTITY
ALTER TABLE TestIdentity
DROP COLUMN ID;
GO

-- Rename new column
EXEC sp_rename 'TestIdentity.ID2', 'ID', 'COLUMN';
GO

INSERT INTO TestIdentity (Value)
VALUES (11),(12),(13),(14),(15);
GO

SELECT * FROM TestIdentity;
GO

DROP TABLE TestIdentity;
GO





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Welsh Corgi
Welsh Corgi
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5164 Visits: 4869
You are right you can't set the Identity Property to true.

You can set the identity property to false by simply going into design mode and set the property to false.

I just wanted to know how to do it in T-SQL.

I guess I could find out how to do so by setting up a trace.

Thank you.

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
Welsh Corgi (5/20/2014)
You are right you can't set the Identity Property to true.

You can set the identity property to false by simply going into design mode and set the property to false.

I just wanted to know how to do it in T-SQL.

I guess I could find out how to do so by setting up a trace.

Thank you.

SSMS will create a new table, copy the data across, delete your original table and rename the new one.



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
MMartin1
MMartin1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1100 Visits: 2014
Where do you see the identity property for a column? I did not see it under sp_columns.

----------------------------------------------------
How to post forum questions to get the best help
Welsh Corgi
Welsh Corgi
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5164 Visits: 4869
Sean Pearce (5/21/2014)
Welsh Corgi (5/20/2014)
You are right you can't set the Identity Property to true.

You can set the identity property to false by simply going into design mode and set the property to false.

I just wanted to know how to do it in T-SQL.

I guess I could find out how to do so by setting up a trace.

Thank you.

SSMS will create a new table, copy the data across, delete your original table and rename the new one.



I did not see the command to change the column to an identity in you example?

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
Welsh Corgi (6/28/2014)
Sean Pearce (5/21/2014)
Welsh Corgi (5/20/2014)
You are right you can't set the Identity Property to true.

You can set the identity property to false by simply going into design mode and set the property to false.

I just wanted to know how to do it in T-SQL.

I guess I could find out how to do so by setting up a trace.

Thank you.

SSMS will create a new table, copy the data across, delete your original table and rename the new one.



I did not see the command to change the column to an identity in you example?

There isn't one.

CREATE TABLE NewTable (ID INT IDENTITY(1, 1));
GO
SET IDENTITY_INSERT NewTable ON;
GO
INSERT INTO NewTable (ID)
SELECT * FROM OldTable;
GO
SET IDENTITY_INSERT NewTable OFF;
GO
DROP TABLE OldTable;
GO
EXEC sp_rename 'NewTable', 'OldTable';
GO



And Vice Versa



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search