drop procedure makePKScript go create procedure makePKScript @dbName varchar(30) as -- if no pkey make script to add one declare @sql nvarchar(2000) set @sql = 'if not exists (select 1 from sysindexes where status&2048=2048 and id = object_id(''''?'''')) print ''''alter table ? add rid int identity constraint pk_'''' + replace(replace(''''?'''',''''[dbo].['''',''''''''),'''']'''','''''''') + ''''__rid primary key''''' set @sql = 'exec sp_msforeachtable '+ '''' + @sql + '''' set @sql = 'use ' + @dbName + ' ' + @sql print 'use ' + @dbName exec sp_executesql @sql