我有@db_out = 'aux.dbo.some_table_name'
,我不知道如何删除,根据该变量创建:
IF OBJECT_ID(@db_out) IS NOT NULL DROP TABLE "@db_out" - not working
CREATE TABLE "@db_out" .... etc
它创建了master.dbo。@ dbo_out
如何使用该变量创建该表或对其进行验证并删除它?
答案 0 :(得分:3)
您必须在varchar变量中构建语句并执行它:
declare @strSql as varchar(max)
IF OBJECT_ID(@db_out) IS NOT NULL
BEGIN
EXEC ('DROP TABLE [' + @db_out + ']')
END
set @strSql = 'CREATE TABLE [' + @db_out + '] (' -- Etc
EXEC (@strSql)
答案 1 :(得分:1)
您需要编写动态SQL。
SET @sql = 'IF OBJECT_ID(@db_out) IS NOT NULL DROP TABLE ' + @db_out + '; '
SET @sql = @sql + 'CREATE TABLE ' + @db_out + ' (...)'
EXEC(@sql)