一鍵生成索引新語句

2021-06-06 13:49:42 字數 4834 閱讀 4002

select d.name

,c.name 

,a.name

,a.[type_desc]  ,

'begin try

if object_id('''+quotename(d.name)+'.'+quotename(c.name)+''') is not null and

not exists(select 1 from sys.indexes

where name ='''+a.name+''')

'+(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) +'

end try

begin catch

print  error_message()

end catch

'    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))

一鍵生成官網 全

你好!不管你是計算機小白或者是老手,這些知識都能對您用幫助!1 阿里雲伺服器一台 centos7.4 64 最少1核2g 並開通oss,並準備好解析好的網域名稱 2 安全組開放80埠 22埠 3 遠端登陸伺服器 4 命令列鍵入 大概60分鐘左右完成安裝 wget p root wget p root...

主鍵 唯一鍵 唯一索引區別

主鍵 1.可以定義一列或多列為主鍵。不允許空 null 主健可作外健,唯一索引不可 2.定義乙個主鍵將自動建立主鍵索引,主鍵索引是唯一索引的特殊型別。唯一鍵 唯一性約束 1.唯一性約束用來限制不受主鍵約束的列上的資料的唯一性,用於作為訪問某行的可選手段,指定列上都不允許有相同的值,允許空 null ...

swift指令碼程式設計 一鍵生成AppIcon

首先你要確定你的mac上的swift版本 swift version複製 我電腦上的執行結果是這樣的 然後就可以用xcode建乙個swift檔案來編寫swift指令碼了,不過單獨建乙個swift檔案,xcode編輯起來非常不友好,我的方案是建乙個在mac上執行的command line tool工程...