Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Change Identity Property in T-SQL Expand / Collapse
Author
Message
Posted Tuesday, May 20, 2014 3:27 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1572544
Posted Tuesday, May 20, 2014 6:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 21, 2014 2:56 AM
Points: 2,603, Visits: 2,061
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."
Post #1572611
Posted Tuesday, May 20, 2014 8:32 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 825, Visits: 2,659
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
Post #1572703
Posted Tuesday, May 20, 2014 8:46 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1572720
Posted Tuesday, May 20, 2014 10:30 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 825, Visits: 2,659
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
Post #1572788
Posted Tuesday, May 20, 2014 10:20 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1572993
Posted Wednesday, May 21, 2014 1:29 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 825, Visits: 2,659
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
Post #1573022
Posted Friday, June 27, 2014 6:29 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 6:44 PM
Points: 317, Visits: 820
Where do you see the identity property for a column? I did not see it under sp_columns.
Post #1587082
Posted Saturday, June 28, 2014 6:27 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1587134
Posted Sunday, June 29, 2014 10:49 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 825, Visits: 2,659
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
Post #1587435
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse