我基本上尝试创建一个应该执行以下操作的动态sql脚本
IF EXISTS(SELECT * FROM sys.indexes WHERE object_id = object_id('FinData2000_1') AND NAME = 'idx_FinData2000_1')
drop index idx_FinData2000_1 on FinData2000_1
以下是未解析的动态脚本
SET @sql = 'IF EXISTS(SELECT * FROM sys.indexes WHERE object_id = object_id('
+ @tableName + '') AND NAME = 'idx_' + @tableName + '
drop index' + @tableName + '.idx_' + @tableName;
答案 0 :(得分:1)
您的动态字符串不正确,请尝试:
Id 1 : (1.5, (2.5, 3.0))
Id 2 : (1.5, (2.5, 4.0))
输出:print(@SQL):
SET @sql = 'IF EXISTS(SELECT * FROM sys.indexes WHERE object_id = object_id('''
+ @tableName + ''') AND NAME = ''idx_' + @tableName + '''
drop index idx_' + @tableName + ' ON ' + @tableName;
答案 1 :(得分:0)
动态查询
declare @tablename varchar(100) = 'tablename'
declare @indexname varchar (100) = 'indexname'
declare @query varchar(max) = 'IF EXISTS(SELECT * FROM sys.indexes WHERE object_id = object_id('+@tablename+') AND NAME = '+@indexname+')
drop index '+@indexname+' on '+@tablename+''
适用于您传递的任何表和索引名称