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

Alter Column with Identity Expand / Collapse
Author
Message
Posted Thursday, August 19, 2010 1:14 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: Wednesday, June 05, 2013 1:01 AM
Points: 872, Visits: 524
Hi All,

The following one is the Table definition.
After inserting some rows ...i want to add identity property to Owner_id.
So please give me alter statement.

CREATE TABLE [dbo].[Testowners](
[owner_id] [int] NULL,
[owname] [varchar](15) NULL
)


INSERT INTO Testowners
Select 1,'asas'

Select * from Testowners
Post #971648
Posted Thursday, August 19, 2010 4:51 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, February 01, 2012 6:49 AM
Points: 178, Visits: 463
Hello,
I get a script similar to below when I tried your requests using SSMS

I hope that helps,

BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Testowners
(
owner_id int NOT NULL IDENTITY (1, 1),
owname varchar(15) NULL
)
GO
SET IDENTITY_INSERT dbo.Tmp_Testowners ON
GO
INSERT INTO dbo.Tmp_Testowners (owner_id, owname)
SELECT owner_id, owname FROM dbo.Testowners
GO
SET IDENTITY_INSERT dbo.Tmp_Testowners OFF
GO
DROP TABLE dbo.Testowners
GO
EXECUTE sp_rename N'dbo.Tmp_Testowners', N'Testowners', 'OBJECT'
GO
COMMIT

This solution requires creation of a new table which is same as initial table except that will have an identity column set while it is being created


Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Post #971753
Posted Thursday, August 19, 2010 6:01 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, October 31, 2012 5:47 AM
Points: 457, Visits: 234
-> Open table design in SSMS.
-> Select "Owner_Id" column and see the property at bottom side.
-> You can find "Identity specification". set it to "Yes".

That's it.
Post #971783
Posted Thursday, August 19, 2010 6:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 11,789, Visits: 28,063
I'd recommend the method Krishnraj suggested; this is especially true with big tables with lots of constraints or foreign keys or other dependancies....the GUI does all the grunt work behind the scenes.

for reference, you cannot alter a column to turn on the identity property once it has been created. you have to rebuild the table, which is exactly what the gui would do behind the scenes.

the GUI will do the following:
· build a new table to the correct specification
· migrate the data fromt he old table to the new.
· drop all the constraints against the original table
· recreate all the constraints against the new table
· drop the original table
· rename the new table to the original name

you can ADD a NEW column with the identity property, but not alter an existing:


CREATE TABLE [dbo].[Testowners](
[owner_id] [int] NULL,
[owname] [varchar](15) NULL
)
--fails: not allowed to alter an existing and add the identity property
ALTER TABLE [Testowners] ALTER COLUMN [owner_id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
--allowed , if the column didn't exist
ALTER TABLE [Testowners] ADD [owner_id2] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #971806
Posted Thursday, August 19, 2010 11:01 PM


SSC Eights!

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

Group: General Forum Members
Last Login: Wednesday, June 05, 2013 1:01 AM
Points: 872, Visits: 524
Hi,

-> Open table design in SSMS.
-> Select "Owner_Id" column and see the property at bottom side.
-> You can find "Identity specification". set it to "Yes".

Thanks for Info.....

It wont work.....It will ask for Dropping of object & Re-Create of the same object.

Regards,
Pulivarthi
Post #972282
Posted Friday, August 20, 2010 12:26 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, October 31, 2012 5:47 AM
Points: 457, Visits: 234
Hi,

For that follow the below steps:

-> Go to "Tools" menu and click on "Options"
-> In the option window, click on "Designers".
-> Here now uncheck the checkbox which says: Prevent saving changes that require table re-creation

But i want to say one thing as Lowell said in the previous post that the GUI does lot of grunt work behind the scenes.
So I recommend u follow it.
Thanks Lowell for sharing that information with us. Thank u very much...
Post #972302
Posted Friday, March 29, 2013 10:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, March 30, 2013 12:20 PM
Points: 1, Visits: 3
alter table [ishchiler] alter column [ishchi_kod] [int] identity(1,1)


problems

Incorrect syntax near the keyword 'identity' how to problems?


help me e-mail adress celal85@mail.ru
Post #1437021
Posted Friday, March 29, 2013 11:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 11,789, Visits: 28,063
note: two year old thread.

the answer is right here, so re-read the whole thread, and use the GUI instead;
your error is mentioned in my post above:
[quote]
--fails: not allowed to alter an existing column to add the identity property
ALTER TABLE [Testowners] ALTER COLUMN [owner_id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY

[/code]


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1437023
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse