日期:2014-05-18 浏览次数:20699 次
select top 0 * into b from a
declare ap scroll cursor for
select name from sysobjects where xtype='U'
declare @tab varchar(200),@sql varchar(5000)
open ap
fetch first from ap into @tab
while(@@FETCH_STATUS<>-1)
begin
select @sql='alter table ['+@tab+'] add constraint ['
+name+'] '
from sys.objects
where parent_object_id=object_id(@tab) and [type]='PK'
select @sql=@sql+' primary key('+
(select cast(
(select name+','
from (select distinct c.name
from sys.indexes a
inner join sys.index_columns b
on a.[object_id]=b.[object_id]
inner join sys.columns c
on b.[object_id]=c.[object_id]
and b.index_column_id=c.column_id
where a.[object_id]=object_id(@tab)
and a.is_primary_key=1) t
for xml path('')) as varchar(20)))
select @sql=left(@sql,len(@sql)-1)+') '
-- 打印
print @sql
fetch next from ap into @tab
end
close ap
deallocate ap