select d.name
,c.name
,a.name
,a.[type_desc] ,
(case
when a.is_primary_key=1
then
'
ALTER TABLE '+quotename(d.name)+'.'+quotename(c.name)+' ADD CONSTRAINT '+quotename(a.name)+' PRIMARY KEY '+a.[type_desc]+'
(
'+
stuff(
(
select ','+t2.name+' '+(case when t1.is_descending_key=1 then 'DESC' else 'ASC' end) from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal>0
order by t1.index_column_id
for xml path('')
),1,1,'')
+'
)
' collate Chinese_PRC_CI_AI_WS
when a.is_unique=1
then
'
ALTER TABLE '+quotename(d.name)+'.'+quotename(c.name)+' ADD CONSTRAINT '+quotename(a.name)+' UNIQUE '+
convert(varchar,a.[type_desc])+'
(
'+
stuff(
(
select ','+t2.name+' '+(case when t1.is_descending_key=1 then 'DESC' else 'ASC' end) from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal>0
order by t1.index_column_id
for xml path('')
),1,1,'')
+'
)
'
+
(case when exists
(select 1 from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal=0)
then 'INCLUDE('+
stuff(
(
select ','+t2.name from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal=0
order by t1.index_column_id
for xml path('')
),1,1,'')+')'
else '' end)
collate Chinese_PRC_CI_AI_WS
else
'
CREATE '+a.[type_desc]+' INDEX '+quotename(a.name)+' ON '+quotename(d.name)+'.'+quotename(c.name)+'
(
'+
stuff(
(
select ','+t2.name+' '+(case when t1.is_descending_key=1 then 'DESC' else 'ASC' end) from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal>0
order by t1.index_column_id
for xml path('')
),1,1,'')
+'
)
'
+
(case when exists
(select 1 from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal=0)
then 'INCLUDE('+
stuff(
(
select ','+t2.name from sys.index_columns t1
inner join sys.all_columns t2 on t1.object_id=a.object_id and t1.index_id=a.index_id
and t1.object_id=t2.object_id and t1.column_id=t2.column_id and t1.key_ordinal=0
order by t1.index_column_id
for xml path('')
),1,1,'')+')'
else '' end)
collate Chinese_PRC_CI_AI_WS
end) ad,
(CASE WHEN a.is_primary_key=1 or a.is_unique=1
THEN
'
begin try
ALTER TABLE '+quotename(d.name)+'.'+quotename(c.name)+' DROP CONSTRAINT '+quotename(a.name)+'
end try
begin catch
print ERROR_MESSAGE()
end catch
'
ELSE
'
begin try
if object_id('''+quotename(d.name)+'.'+quotename(c.name)+''') is not null and
exists(select 1 from sys.indexes
where name ='''+a.name+''')
drop index '+quotename(a.name)+' on '+quotename(d.name)+'.'+quotename(c.name)+'
end try
begin catch
print ERROR_MESSAGE()
end catch
'END) del
,a.* from sys.indexes a
inner join sys.all_objects c on a.object_id=c.object_id and c.type='u'
inner join sys.schemas d on c.schema_id=d.schema_id and a.type_desc in ('CLUSTERED','NONCLUSTERED')
--WHERE exists(select * from [master].[dbo].[UnionALL]
--where quotename(sch)+'.'+quotename(tbl)=quotename(d.name)+'.'+quotename(c.name)
-- and a.keys like '%'+col+'%')
where exists
(select * from sys.all_columns s1
inner join sys.index_columns s2 on s1.object_id=s2.object_id and s1.column_id=s2.column_id and s1.object_id=a.object_id
and exists(select * from [master].[dbo].[UnionALL] where db=db_name()
and tbl=c.name and sch=d.name and col=s1.name))