In this post we will learn how to drop identity property of a column in SQL Server Table.
We will follow below steps.
We will follow below steps.
- Add a new column with TestID to existing Table
- Update the records from Id ( Identity enable Column) to TestID (Newly Added) Column.
- Drop the Id ( Identity Enable Column) from Table
- Rename the Newly Added Column ( TestID) to Id.
--Create Table with Identity PropertyCREATE TABLE dbo.Employee ( Id INT IDENTITY(1,1), Name VARCHAR(10))GO
--Insert the record after creating Table with Identity Property on Id ColumnINSERT INTO dbo.Employee VALUES('Shahzad')GO
--Run to See the DataSELECT * FROM dbo.Employee
--Find out all the columns for all the tables on which Identity Property is enabledSELECT OBJECT_NAME(OBJECT_ID) AS TableName,name AS ColumnName FROM sys.columns
WHERE is_identity=1
/** Drop Identity ********/
--Add a new column with any nameALTER TABLE dbo.Employee
ADD TestId INT
--Update the Records in newly Added column , in our case TestIDUPDATE dbo.Employee
SET TestId=Id
--Drop Identity ColumnALTER TABLE dbo.Employee
DROP COLUMN Id
--Rename the newly Added Column to Identity Column you had at first.EXEC sp_rename 'dbo.Employee.TestId','Id','COLUMN'
Video Demo : How to drop Identity Property of a Column in SQL Server Table



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.