Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to disable is identity of a column in table with the help of sql querry plz help
Posted

I think this will help you:
SQL
SET IDENTITY_INSERT TableName ON|OFF
 
Share this answer
 
Comments
[no name] 26-Jun-12 0:38am    
its not working
[no name] 26-Jun-12 0:39am    
the id is stinll generating by itself
Dear Vijay

The above code will only disable the identity, if you have to remove the identity altogether there is the steps below.

--If other tables' foreign keys point to the IDENTITY column, drop them. Here, the script drops
--the foreign key pointing to Orders2.OrderID from OrderDetails2.
ALTER TABLE OrderDetails2
DROP CONSTRAINT FK_OrderDetails2_Orders2

--If a primary key exists on the IDENTITY column, drop the primary key constraint the way this
--script drops the primary key from Orders2.
ALTER TABLE Orders2
DROP Constraint PK_Orders2

--Add another column with the same data type as the IDENTITY column to Orders 2 and allow
--NULLs.
ALTER TABLE Orders2
ADD new_OrderID int NULL

--Update the new column with the values of the IDENTITY column.
UPDATE Orders2
SET new_OrderID = OrderID

--If the new column doesn't permit NULLs, alter the column to NOT NULL.
ALTER TABLE Orders2
ALTER COLUMN new_OrderID int NOT NULL

--Drop the IDENTITY column.
ALTER TABLE Orders2
DROP COLUMN OrderID

--Rename the new column to the dropped IDENTITY column's name.
EXEC sp_rename 'Orders2.new_OrderID', 'OrderID', 'COLUMN'

--If a primary key exists on the new column, recreate the key. In this case you recreate the
--primary key on Orders2.
ALTER TABLE Orders2
ADD CONSTRAINT PK_Orders2 PRIMARY KEY(OrderID)

--On other tables, recreate any foreign keys that originally pointed to the old IDENTITY column
--and point them to the new column. Here, you recreate the foreign key on OrderDetails2.
ALTER TABLE OrderDetails2 WITH NOCHECK
ADD CONSTRAINT FK_OrderDetails2_Orders2
FOREIGN KEY(OrderID)
REFERENCES Orders2(OrderID)
 
Share this answer
 
Comments
[no name] 26-Jun-12 0:48am    
i just want to change the isidentity propert of the column of a table from yes to no
SQL
SET IDENTITY_insert  tbbranchheadoffice on
insert into tbbranchheadoffice(id,name,dob,fathersname)values(85,'Vk','dsfsd','dsfs')
 
Share this answer
 
hey dude..

see this link

surely we you will get answer.. :)
 
Share this answer
 
v2
Comments
ssd_coolguy 26-Jun-12 5:30am    
hey.. now i have updated link...
just check it..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900