No Primary Keys ? No Problem !!!

Declare @prefix as nvarchar(11);
Declare @suffix1 as nvarchar(100);
Declare @suffix2 as nvarchar(100);

set @prefix=’ALTER TABLE’;

set @suffix1= ‘ ADD ID int NOT NULL IDENTITY(1,1) CONSTRAINT pk_’;
set @suffix2= ‘ PRIMARY KEY NONCLUSTERED ‘;

select distinct(sys.tables.name) , @prefix +’ ‘+ sys.tables.name + @suffix1+sys.tables.name+@suffix2
from sys.tables inner join syscolumns on
sys.tables.object_id=syscolumns.id
order by sys.tables.name asc

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s