Creating SQL table using dynamic variable name

前端 未结 5 939
滥情空心
滥情空心 2021-02-05 17:23

I want to create backup SQL tables using variable names.

something along the lines of

DECLARE @SQLTable Varchar(20) 
SET @SQLTable = \'SomeTableName\' +         


        
5条回答
  •  死守一世寂寞
    2021-02-05 17:57

    DECLARE @MyTableName nvarchar(20);
    DECLARE @DynamicSQL nvarchar(1000);
    
    SET @MyTableName = "FooTable";
    
    
    SET @DynamicSQL = N'SELECT * INTO ' + @MyTableName + ' FROM BarTable';
    
    exec @DynamicSQL;
    

    this query is correct but just use single quote at the ("FooTable")='FooTable'

提交回复
热议问题