这会出现以下错误:
Msg 207,Level 16,State 1,Line 1
列名称无效' Testingwithdynamicsql'。
现在为什么会这样,如果我打印我的sql它对我来说似乎有效,我会改变它以使其有效?
Declare @tablename varchar(500), @sql varchar(max)
set @tablename = 'Testingwithdynamicsql'
set @sql = 'IF OBJECT_ID('+@tablename+', ''U'') IS NOT NULL
DROP TABLE ' + @tablename + ' '
Print @sql
exec(@sql)
答案 0 :(得分:1)
需要引用@tablename
Declare @tablename varchar(500), @sql varchar(max)
set @tablename = 'Testingwithdynamicsql'
set @sql = 'IF OBJECT_ID('''+@tablename+''', ''U'') IS NOT NULL
DROP TABLE ' + @tablename + ' '
Print @sql