Posted on Leave a comment

EXEC – INSERT

You can return the results of a dynamic sql statement into a temp table or table variable:

DECLARE @tab TABLE ( id int, val varchar(100) )
DECLARE @sql varchar(8000)

SET @sql = "SELECT id, name FROM dbo.mytable"

INSERT INTO @tab ( id, val )
EXEC( @sql )

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.