Creating SQL table using dynamic variable name

前端 未结 5 952
滥情空心
滥情空心 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:58

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

提交回复
热议问题