I need to INSERT into a table-valued variable several rows that come from a dynamically generated SELECT statement, that is INSERT INTO ... SELECT ...
I'm trying to do it via sp_executesql. How to return this filled table-valued variable from the sp_executesql?
DECLARE @Data as MyTableType
DECLARE @stmt as nvarchar(max)
SET @stmt = '
DECLARE @Data as MyTableType
INSERT INTO @Data VALUES (''test1'',''test2'') --in fact this will be INSERT INTO @Data SELECT ...
'
execute sp_executesql @stmt, N'@Data MyTableType output', @Data = @Data output
Error:
The table variable "@Data" can not be passed to a stored procedure with the OUTPUT option.
However, it accepts the other way around, that is if @Data is used as input (replacing OUTPUT for READONLY).