• sqlserver获得数据库非聚集索引的代码


     创建Index

    	DECLARE @zindex_sql NVARCHAR(max);
    	SET @zindex_sql = N'';
    
    	SELECT @zindex_sql = @zindex_sql + 
    	CASE WHEN r.is_primary_key=1 THEN
    		N'ALTER TABLE [' + r.schemas_name  +  '].[' + r.tablename + '] ADD CONSTRAINT [' + r.INDEX_name + N'] PRIMARY KEY ' + r.type_desc + N' ' +
    		r.index_fields + 
    		CASE WHEN r.include_fields<>N'' THEN N' INCLUDE ' + r.include_fields ELSE N'' END  +
    		 N' ON [' + r.data_spaces_name  + N'];' 
    	ELSE
    		N'CREATE ' + 
    		CASE WHEN r.is_unique=1 THEN N'UNIQUE ' ELSE N'' END  
    		 + r.type_desc + N' INDEX [' + r.INDEX_name + N'] ON [' + r.schemas_name  +  '].[' + r.tablename + '] ' + r.index_fields + 
    		CASE WHEN r.include_fields<>N'' THEN N' INCLUDE ' + r.include_fields ELSE N'' END  +
    		 N' ON [' + r.data_spaces_name  + N'];' 
    	 END 
    	FROM (
    	SELECT b.object_id AS tid,b.schemas_name, b.name AS tablename,c.name as INDEX_name,
    	REPLACE(REPLACE(REPLACE((SELECT N'[' + y.name + N']' + CASE WHEN x.is_descending_key=1 THEN N' DESC' ELSE N'' END  AS field_name  
    	FROM sys.index_columns x 
    	 INNER JOIN  sys.columns y ON y.object_id = x.object_id AND y.column_id = x.column_id
    	 WHERE x.object_id=c.object_id AND x.index_id=c.index_id AND x.is_included_column=0 ORDER BY x.key_ordinal FOR XML PATH('')) ,N'</field_name><field_name>',N',')
    	 ,N'<field_name>',N'(')
    	 ,N'</field_name>',N')')
    	  AS index_fields, 
    	REPLACE(REPLACE(REPLACE((SELECT N'[' + y.name + N']' + CASE WHEN x.is_descending_key=1 THEN N' DESC' ELSE N'' END  AS field_name  
    	FROM sys.index_columns x 
    	 INNER JOIN  sys.columns y ON y.object_id = x.object_id AND y.column_id = x.column_id
    	 WHERE x.object_id=c.object_id AND x.index_id=c.index_id AND x.is_included_column=1 ORDER BY x.key_ordinal FOR XML PATH('')) ,N'</field_name><field_name>',N',')
    	 ,N'<field_name>',N'(')
    	 ,N'</field_name>',N')')
    	  AS include_fields, 
    	c.* 
    	,data_spaces.name AS  data_spaces_name
    	FROM (
    	SELECT a.*,schemas.name AS schemas_name FROM sys.tables AS a
    	INNER JOIN sys.schemas ON schemas.schema_id = a.schema_id
    	INNER JOIN (
    	SELECT * FROM [XServerPREDEPLOY].DB1.sys.tables AS [Tables] WHERE Tables.name LIKE N'tbl%') AS b
    	ON b.name = a.name COLLATE SQL_Latin1_General_CP1_CI_AS
    	)b
    	INNER JOIN sys.indexes c ON c.object_id = b.object_id
    	INNER JOIN sys.data_spaces ON data_spaces.data_space_id = c.data_space_id
    	WHERE c.type IN (1,2)
    	) r
    	;
    

      

    https://www.cnblogs.com/shiyh/p/9431786.html

     设置数据库collation

    	DECLARE @sql NVARCHAR(max);
    	SET @sql = N'';
    	SELECT 
    		@sql = @sql + N' alter table ' +	[Tables].name  + N' alter column  ' + [Columns].name + N' ' + Types.name  + N'(' + CASE WHEN [Columns].max_length=-1 THEN N'max' else CAST([Columns].max_length/2 AS NVARCHAR(5)) END + N') collate DATABASE_DEFAULT;' + NCHAR(10) + NCHAR(13)           
        FROM sys.tables AS [Tables]
            INNER JOIN sys.columns AS [Columns]
                ON [Tables].object_id = [Columns].object_id
            INNER JOIN sys.types AS [Types]
                ON [Columns].system_type_id = [Types].system_type_id
                   AND is_user_defined = 0
                   AND [Types].name <> 'sysname'
            LEFT OUTER JOIN sys.extended_properties AS [Properties]
                ON [Properties].major_id = [Tables].object_id
                   AND [Properties].minor_id = [Columns].column_id
                   AND [Properties].name = 'MS_Description'
        WHERE Columns.collation_name IS NOT NULL AND CHARINDEX(N'tbl' ,[Tables].name)=1
    	ORDER BY [Tables].name,[Columns].name
    
    	SELECT @sql;
    

      

    删除Index

    DECLARE @zdrop_index_sql NVARCHAR(max);
    SET @zdrop_index_sql = N'';
    
    SELECT @zdrop_index_sql = @zdrop_index_sql + 
    CASE WHEN r.is_primary_key=1 THEN
    N'ALTER TABLE [' + r.schemas_name  +  '].[' + r.tablename + '] DROP CONSTRAINT [' + r.INDEX_name + N'];' 
    ELSE 
    N'DROP INDEX [' + r.INDEX_name + N'] ON [' + r.schemas_name  +  '].[' + r.tablename + ']; '
    end
    FROM (
    SELECT b.object_id AS tid,b.schemas_name, b.name AS tablename,c.name as INDEX_name,c.is_primary_key
    FROM (
    SELECT a.*,schemas.name AS schemas_name FROM sys.tables AS a
    INNER JOIN sys.schemas ON schemas.schema_id = a.schema_id
    INNER JOIN (
    SELECT * FROM [XServerPREDEPLOY].DB1.sys.tables AS [Tables] WHERE Tables.name LIKE N'tbl%') AS b
    ON b.name = a.name COLLATE SQL_Latin1_General_CP1_CI_AS
    )b
    INNER JOIN sys.indexes c ON c.object_id = b.object_id
    WHERE c.type IN (1,2)
    ) r
    ;
    
    SELECT @zdrop_index_sql;
    

      

     

  • 相关阅读:
    crazyflie2.0 RCC时钟知识
    quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
    [Swift通天遁地]一、超级工具-(16)使用JTAppleCalendar制作美观的日历
    [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II
    [SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries
    [Swift通天遁地]一、超级工具-(15)使用SCLAlertView制作强大的Alert警告窗口和Input编辑窗口
    [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
    [Swift通天遁地]一、超级工具-(14)使用SweetAlert制作漂亮的自定义Alert窗口
    [Swift]关键字:Self、self与super
    [Swift]LeetCode964. 表示数字的最少运算符 | Least Operators to Express Number
  • 原文地址:https://www.cnblogs.com/coolyylu/p/11350152.html
Copyright © 2020-2023  润新知