Use more than one sql statement in EXEC : EXEC « Transact SQL « SQL Server / T-SQL Tutorial






5> DECLARE @schema AS VARCHAR(1000), @insert AS VARCHAR(1000);
6> SET @schema = 'col1 INT, col2 DECIMAL(10, 2)';
7> SET @insert = 'INSERT INTO #T42 VALUES(10, 20.30)';
8>
10> CREATE TABLE #T42(dummycol INT);
11>
17> EXEC('
18~ ALTER TABLE #T42 ADD ' + @schema + ';
19~ ALTER TABLE #T42 DROP COLUMN dummycol;
20~
21~ EXEC(''' + @insert + ''')');
22> GO
1>
2> -- Back in the outer level, access #T in a new batch
3> SELECT * FROM #T42;
4> GO
col1        col2
----------- ------------
         10        20.30
1> -- Cleanup
2> DROP TABLE #T42;
3> GO








20.19.EXEC
20.19.1.The syntax of the EXEC statement
20.19.2.EXECUTE AS
20.19.3.Dynamic Selects
20.19.4.A stored procedure with dynamic execution by using EXEC
20.19.5.Sending Variable Information to an EXEC() Call
20.19.6.Switch database in EXEC
20.19.7.Statement Limit
20.19.8.Parse exception in another level of scope: Exception in EXEC
20.19.9.Execute a dynamic statement and return the result with OUTPUT
20.19.10.Use more than one sql statement in EXEC
20.19.11.Insert into with EXEC
20.19.12.Using EXEC () to Execute a Dynamic TOP n Query