濡備綍鑾峰彇涓€涓暟鎹簱鐨勬墍鏈夊缓琛ㄨ鍙ヤ笌鍒涘缓绱㈠紩鐨勮鍙ワ紵
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濡備綍鑾峰彇涓€涓暟鎹簱鐨勬墍鏈夊缓琛ㄨ鍙ヤ笌鍒涘缓绱㈠紩鐨勮鍙ワ紵相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%95%b0%e6%8d%ae%e5%ba%93%e5%90%8d' title='鏁版嵁搴撳悕'>鏁版嵁搴撳悕
one 鐢熸垚 nullable 鍒涘缓绱㈠紩 temp position gif 娣诲姞濡備綍鑾峰彇涓€涓暟鎹簱鐨勬墍鏈夊缓琛ㄨ鍙ヤ笌鍒涘缓绱㈠紩鐨勮鍙ワ紵
鐢熸垚寤鸿〃璇彞鎿嶄綔
銆€銆€SSMS涓?nbsp; 鍙冲嚮鏁版嵁搴?銆嬬敓鎴愯剼鏈?銆嬮€夋嫨浠呴檺鏋舵瀯
鍒╃敤T-SQL鐢熸垚寤鸿〃璇彞
--鐢熸垚褰撳墠鏁版嵁搴撲笅鎵€鏈夎〃鐨勫垱寤鸿剼鏈?/span> select 鈥?/span>create table [鈥?/span> + so.name + 鈥?/span>] (鈥?/span> + o.list + 鈥?/span>)鈥?/span> + CASE WHEN tc.Constraint_Name IS NULL THEN 鈥樷€?/span> ELSE 鈥?/span>ALTER TABLE 鈥?/span> + so.Name + 鈥?/span> ADD CONSTRAINT 鈥?/span> + tc.Constraint_Name + 鈥?/span> PRIMARY KEY 鈥?/span> + 鈥?/span> (鈥?/span> + LEFT(j.List, Len(j.List)-1) + 鈥?/span>)鈥?/span> END from sysobjects so cross apply (SELECT 鈥?/span> [鈥?/span>+column_name+鈥?/span>] 鈥?/span> + data_type + case data_type when 鈥?/span>sql_variant鈥?/span> then 鈥樷€?/span> when 鈥?/span>text鈥?/span> then 鈥樷€?/span> when 鈥?/span>decimal鈥?/span> then 鈥?/span>(鈥?/span> + cast(numeric_precision_radix as varchar) + 鈥?/span>, 鈥?/span> + cast(numeric_scale as varchar) + 鈥?/span>)鈥?/span> else coalesce(鈥?/span>(鈥?/span>+case when character_maximum_length = -1 then 鈥?/span>MAX鈥?/span> else cast(character_maximum_length as varchar) end +鈥?/span>)鈥?/span>,鈥樷€?/span>) end + 鈥?/span> 鈥?/span> + case when exists ( select id from syscolumns where object_name(id)=so.name and name=column_name and columnproperty(id,name,鈥?/span>IsIdentity鈥?/span>) = 1 ) then 鈥?/span>IDENTITY(鈥?/span> + cast(ident_seed(so.name) as varchar) + 鈥?/span>,鈥?/span> + cast(ident_incr(so.name) as varchar) + 鈥?/span>)鈥?/span> else 鈥樷€?/span> end + 鈥?/span> 鈥?/span> + (case when IS_NULLABLE = 鈥?/span>No鈥?/span> then 鈥?/span>NOT 鈥?/span> else 鈥樷€?/span> end ) + 鈥?/span>NULL 鈥?/span> + case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 鈥?/span>DEFAULT 鈥?/span>+ information_schema.columns.COLUMN_DEFAULT ELSE 鈥樷€?/span> END + 鈥?/span>, 鈥?/span> from information_schema.columns where table_name = so.name order by ordinal_position FOR XML PATH(鈥樷€?/span>)) o (list) left join information_schema.table_constraints tc on tc.Table_name = so.Name AND tc.Constraint_Type = 鈥?/span>PRIMARY KEY鈥?/span> cross apply (select 鈥?/span>[鈥?/span> + Column_Name + 鈥?/span>], 鈥?/span> FROM information_schema.key_column_usage kcu WHERE kcu.Constraint_Name = tc.Constraint_Name ORDER BY ORDINAL_POSITION FOR XML PATH(鈥樷€?/span>)) j (list) where xtype = 鈥?/span>U鈥?/span> AND name NOT IN (鈥?/span>dtproperties鈥?/span>)
鐢熸垚琛ㄧ储寮曞垱寤恒€佸垹闄よ鍙?/p>
--鐢熸垚琛ㄧ储寮曠殑鍒涘缓鍒犻櫎璇彞 ; WITH TB AS ( SELECT TB.object_id , Schema_name = Sch.name , table_name = TB.name FROM sys.tables TB INNER JOIN sys.schemas Sch ON TB.schema_id = Sch.schema_id WHERE TB.is_ms_shipped = 0 ), IXC AS ( SELECT IXC.object_id , IXC.index_id , IXC.index_column_id , IXC.is_descending_key , IXC.is_included_column , column_name = C.name FROM SYS.index_columns IXC INNER JOIN SYS.columns C ON IXC.object_id = C.object_id AND IXC.column_id = C.column_id ), IX AS ( SELECT IX.object_id , index_name = IX.name , index_type_desc = IX.type_desc , IX.is_unique , IX.is_primary_key , IX.is_unique_constraint , IX.is_disabled , index_columns_TEMP = STUFF(IXC_COL.index_columns, 1, 1, N鈥樷€?/span>) , index_columns = CASE WHEN IXC_COL_INCLUDE.index_columns_includes IS NOT NULL THEN STUFF(LEFT(IXC_COL.index_columns, DATALENGTH(IXC_COL.index_columns) - DATALENGTH(IXC_COL_INCLUDE.index_columns_includes)), 1, 1, N鈥樷€?/span>) ELSE STUFF(IXC_COL.index_columns, 1, 1, N鈥樷€?/span>) END , index_columns_includes = STUFF(IXC_COL_INCLUDE.index_columns_includes, 1, 1, N鈥樷€?/span>) FROM sys.indexes IX CROSS APPLY ( SELECT index_columns = ( SELECT N鈥?/span>,鈥?/span> + QUOTENAME(column_name) FROM IXC WHERE object_id = IX.object_id AND index_id = IX.index_id ORDER BY index_column_id FOR XML PATH(鈥樷€?/span>) , ROOT(鈥?/span>r鈥?/span>) , TYPE ).value(鈥?/span>/r[1]鈥?/span>, 鈥?/span>nvarchar(max)鈥?/span>) ) IXC_COL OUTER APPLY ( SELECT index_columns_includes = ( SELECT N鈥?/span>,鈥?/span> + QUOTENAME(column_name) FROM IXC WHERE object_id = IX.object_id AND index_id = IX.index_id AND is_included_column = 1 ORDER BY index_column_id FOR XML PATH(鈥樷€?/span>) , ROOT(鈥?/span>r鈥?/span>) , TYPE ).value(鈥?/span>/r[1]鈥?/span>, 鈥?/span>nvarchar(max)鈥?/span>) ) IXC_COL_INCLUDE WHERE index_id > 0 ) SELECT DB_NAME() AS N鈥?/span>鏁版嵁搴撳悕鈥?/span> , TB.Schema_name AS N鈥?/span>鏋舵瀯鈥?/span> , TB.table_name AS N鈥?/span>琛ㄥ悕鈥?/span> , IX.index_name AS N鈥?/span>绱㈠紩鍚?/span>鈥?/span> , IX.index_type_desc AS N鈥?/span>绱㈠紩绫诲瀷鈥?/span> , IX.is_unique AS N鈥?/span>鏄惁鍞竴绱㈠紩鈥?/span> , IX.is_primary_key AS N鈥?/span>鏄惁涓婚敭鈥?/span> , IX.is_unique_constraint AS N鈥?/span>鏄惁鍞竴绾︽潫鈥?/span> , IX.is_disabled AS N鈥?/span>鏄惁绂佺敤绱㈠紩鈥?/span> , IX.index_columns AS N鈥?/span>绱㈠紩鍒?/span>鈥?/span> , IX.index_columns_includes AS N鈥?/span>绱㈠紩鍖呭惈鍒?/span>鈥?/span> , N鈥?/span>CREATE INDEX 鈥?/span> + N鈥?/span>[鈥?/span> + IX.index_name + N鈥?/span>]鈥?/span> + N鈥?/span> ON 鈥?/span> + N鈥?/span>[鈥?/span> + QUOTENAME(DB_NAME()) + N鈥?/span>.鈥?/span> + QUOTENAME(TB.SCHEMA_NAME) + N鈥?/span>.鈥?/span> + QUOTENAME(TB.table_name) + N鈥?/span>(鈥?/span> + IX.index_columns + N鈥?/span>)鈥?/span> + CASE WHEN IX.index_columns_includes IS NOT NULL THEN CHAR(13) + N鈥?/span>INCLUDE (鈥?/span> + IX.index_columns_includes + N鈥?/span>)鈥?/span> ELSE N鈥樷€?/span> END AS N鈥?/span>鍒涘缓绱㈠紩鈥?/span> , N鈥?/span>DROP INDEX 鈥?/span> + QUOTENAME(IX.index_name) + N鈥?/span> ON 鈥?/span> + QUOTENAME(DB_NAME()) + N鈥?/span>.鈥?/span> + QUOTENAME(TB.SCHEMA_NAME) + N鈥?/span>.鈥?/span> + QUOTENAME(TB.table_name) AS N鈥?/span>鍒犻櫎绱㈠紩鈥?/span> FROM TB INNER JOIN IX ON TB.object_id = IX.object_id ORDER BY Schema_name , table_name , IX.index_name
鏌ョ湅琛ㄨ鎯呫€佹暟鎹瓧鍏?/p>
--鏌ョ湅琛ㄧ殑璇︾粏淇℃伅 SELECT 琛ㄥ悕 = CASE WHEN C.column_id = 1 THEN O.name ELSE N鈥樷€?/span> END , 琛ㄨ鏄?= ISNULL(CASE WHEN C.column_id = 1 THEN PTB.[value] END, N鈥樷€?/span>) , 瀛楁搴忓彿 = C.column_id , 瀛楁鍚?= C.name , 涓婚敭 = ISNULL(IDX.PrimaryKey, N鈥樷€?/span>) , 鏍囪瘑 = CASE WHEN C.is_identity = 1 THEN N鈥?/span>鈭?/span>鈥?/span> ELSE N鈥樷€?/span> END , 璁$畻鍒?= CASE WHEN C.is_computed = 1 THEN N鈥?/span>鈭?/span>鈥?/span> ELSE N鈥樷€?/span> END , 绫诲瀷 = T.name , 闀垮害 = C.max_length , 绮惧害 = C.precision , 灏忔暟浣嶆暟 = C.scale , 鍏佽绌?= CASE WHEN C.is_nullable = 1 THEN N鈥?/span>鈭?/span>鈥?/span> ELSE N鈥樷€?/span> END , 榛樿鍊?= ISNULL(D.definition, N鈥樷€?/span>) , 瀛楁璇存槑 = ISNULL(PFD.[value], N鈥樷€?/span>) , 绱㈠紩鍚?= ISNULL(IDX.IndexName, N鈥樷€?/span>) , 绱㈠紩鎺掑簭 = ISNULL(IDX.Sort, N鈥樷€?/span>) , 鍒涘缓鏃堕棿 = O.Create_Date , 淇敼鏃堕棿 = O.Modify_date FROM sys.columns C INNER JOIN sys.objects O ON C.[object_id] = O.[object_id] AND O.type = 鈥?/span>U鈥?/span> AND O.is_ms_shipped = 0 INNER JOIN sys.types T ON C.user_type_id = T.user_type_id LEFT JOIN sys.default_constraints D ON C.[object_id] = D.parent_object_id AND C.column_id = D.parent_column_id AND C.default_object_id = D.[object_id] LEFT JOIN sys.extended_properties PFD ON PFD.class = 1 AND C.[object_id] = PFD.major_id AND C.column_id = PFD.minor_id -- AND PFD.name=鈥楥aption鈥? -- 瀛楁璇存槑瀵瑰簲鐨勬弿杩板悕绉?涓€涓瓧娈靛彲浠ユ坊鍔犲涓笉鍚宯ame鐨勬弿杩? LEFT JOIN sys.extended_properties PTB ON PTB.class = 1 AND PTB.minor_id = 0 AND C.[object_id] = PTB.major_id -- AND PFD.name=鈥楥aption鈥? -- 琛ㄨ鏄庡搴旂殑鎻忚堪鍚嶇О(涓€涓〃鍙互娣诲姞澶氫釜涓嶅悓name鐨勬弿杩? LEFT JOIN -- 绱㈠紩鍙婁富閿俊鎭? ( SELECT IDXC.[object_id] , IDXC.column_id , Sort = CASE INDEXKEY_PROPERTY(IDXC.[object_id], IDXC.index_id, IDXC.index_column_id, 鈥?/span>IsDescending鈥?/span>) WHEN 1 THEN 鈥?/span>DESC鈥?/span> WHEN 0 THEN 鈥?/span>ASC鈥?/span> ELSE 鈥樷€?/span> END , PrimaryKey = CASE WHEN IDX.is_primary_key = 1 THEN N鈥?/span>鈭?/span>鈥?/span> ELSE N鈥樷€?/span> END , IndexName = IDX.Name FROM sys.indexes IDX INNER JOIN sys.index_columns IDXC ON IDX.[object_id] = IDXC.[object_id] AND IDX.index_id = IDXC.index_id LEFT JOIN sys.key_constraints KC ON IDX.[object_id] = KC.[parent_object_id] AND IDX.index_id = KC.unique_index_id INNER JOIN -- 瀵逛簬涓€涓垪鍖呭惈澶氫釜绱㈠紩鐨勬儏鍐?鍙樉绀虹1涓储寮曚俊鎭? ( SELECT [object_id] , Column_id , index_id = MIN(index_id) FROM sys.index_columns GROUP BY [object_id] , Column_id ) IDXCUQ ON IDXC.[object_id] = IDXCUQ.[object_id] AND IDXC.Column_id = IDXCUQ.Column_id AND IDXC.index_id = IDXCUQ.index_id ) IDX ON C.[object_id] = IDX.[object_id] AND C.column_id = IDX.column_id -- WHERE O.name like 鈥?name%鈥?/span> -- 濡傛灉鍙煡璇㈡寚瀹氳〃,鍔犱笂姝ゆ潯浠? ORDER BY O.name , C.column_id
以上是关于濡備綍鑾峰彇涓€涓暟鎹簱鐨勬墍鏈夊缓琛ㄨ鍙ヤ笌鍒涘缓绱㈠紩鐨勮鍙ワ紵的主要内容,如果未能解决你的问题,请参考以下文章