在SQL Server中的查询中测试现有列

时间:2017-11-29 17:36:47

标签: sql-server

我在下面有这个脚本:

---------Part 1--------
SELECT 
    COLUMN_NAME 
FROM 
    (SELECT 
         SC.name as TABLE_SCHEMA,
         t.name as TABLE_NAME,
         ind.name as indexName,
         ind.type_desc AS type_Index,
         SUBSTRING((SELECT ', ' + AC.name
                    FROM DATA.sys.[tables] AS T
                    INNER JOIN DATA.sys.[indexes] I   ON T.[object_id] = I.[object_id]
                    INNER JOIN DATA.sys.[index_columns] IC ON I.[object_id] = IC.[object_id] AND I.[index_id] = IC.[index_id]
                    INNER JOIN DATA.sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id] 
                    WHERE Ind.[object_id] = I.[object_id] AND Ind.index_id = I.index_id AND IC.is_included_column = 0
                    ORDER BY IC.key_ordinal
                    FOR XML PATH('')), 2, 8000) AS COLUMN_NAME
     FROM DATA.sys.indexes ind 
     INNER JOIN DATA.sys.index_columns ic ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id 
     INNER JOIN DATA.sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 
     INNER JOIN DATA.sys.tables t ON ind.object_id = t.object_id
     INNER JOIN DATA.sys.schemas SC ON t.schema_id =  SC.schema_id
     WHERE SC.name ='SCHEMA1') AS SubQ
WHERE 
    COLUMN_NAME IN 
     -------Part 2-------
        (SELECT COLUMN_NAME 
         FROM DATA.INFORMATION_SCHEMA.COLUMNS
         WHERE DATA_TYPE IN ('nchar', 'nvarchar')
           AND TABLE_SCHEMA IN ('SCHEMA1', 'SCHEMA2'))

当我运行第1部分或第2部分时,我得到了最好的列,并且两个结果是相同的,但是当我运行所有脚本时,它不会显示任何内容,也没有错误。

请帮忙吗? 谢谢

0 个答案:

没有答案