Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Add Identity Column

Author  Topic 

scotthanes
Starting Member

6 Posts

Posted - 2007-11-16 : 13:30:57
Does anybody know how I would go about creating a procedure to change the first column of all of my database tables to an identity column. SQL 2005

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-11-16 : 14:59:05
quote:
Originally posted by scotthanes

Does anybody know how I would go about creating a procedure to change the first column of all of my database tables to an identity column. SQL 2005



you can get the "first" column by colid from syscolumns. I would make sure its the same column as what you intend to before running the script. Otherwise you can you can just add an identity column to the table and drop the other column..

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

scotthanes
Starting Member

6 Posts

Posted - 2007-11-16 : 15:32:44
I appreciate you attention. However to tell you the truth I not quite sure where to start. I'm new to SQL 2005 and am familiar with SQl scripting. Any clues would be great.
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-11-16 : 17:54:02
You can do something like this:
 SELECT 'ALTER TABLE ' + Name + ' ADD ' + Name + 'Id IDENTITY(1,1)'
FROM sysobjects
WHERE Type = 'u'


and manually go and drop the column that you wanted to be converted to the identity column.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -