在sqlserver中不正确的EXISTS

时间:2017-11-29 11:08:55

标签: sql-server

我有我的sqlserver脚本,用于测试索引中是否存在列,如果是,则删除索引。 但是当我运行它时,它会显示错误:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

当我更改IN by EXISTS时,会返回此错误:

Incorrect syntax near the keyword 'EXISTS'.

我的脚本是:

while (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 ='SSCI_SALR') as SubQ
GROUP BY COLUMN_NAME) IN ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
                            WHERE DATA_TYPE IN ('nchar', 'nvarchar')
                            and TABLE_SCHEMA IN ('SSCI_SALR', 'SYSTEM_SALR'))
BEGIN

print 'test okkkkk';
IF EXISTS (SELECT *  FROM DATA.SYS.INDEXES WHERE NAME = 'indexName')
Drop Index indexName On DATA.Table_Name;

SELECT
    AlterSql = CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' ALTER COLUMN ', COLUMN_NAME, 
       ' ', SUBSTRING(DATA_TYPE, 2, LEN(DATA_TYPE)), '(', CHARACTER_MAXIMUM_LENGTH, ') ', 
       CASE IS_NULLABLE 
       WHEN 'YES' THEN 'NULL'
       WHEN 'NO' THEN 'NOT NULL'
       ELSE 'ERROR'
       END,
       CHAR(13),
       CHAR(10), 'GO')     
  , *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE IN ('nchar', 'nvarchar')
and TABLE_SCHEMA IN ('SSCI_SALR', 'SYSTEM_SALR')

END;

GO

有人可以告诉我哪里是我的错? 谢谢。

1 个答案:

答案 0 :(得分:1)

第一,使用IF条件而不是While来执行逻辑运算。此外,您的查询是错误的,我这样修改了它;

IF(EXISTS(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 ='SSCI_SALR') as SubQ
              WHERE COLUMN_NAME IN ( SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
                            WHERE DATA_TYPE IN ('nchar', 'nvarchar')
                            and TABLE_SCHEMA IN ('SSCI_SALR', 'SYSTEM_SALR'))
GROUP BY COLUMN_NAME))
begin
   print 'ok'
end