0

我需要将来自动态生成的 SELECT 语句的几行插入到表值变量中,即 INSERT INTO ... SELECT ...

我正在尝试通过 sp_executesql 来实现。如何从 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

错误:

The table variable "@Data" can not be passed to a stored procedure with the OUTPUT option.

但是,它接受另一种方式,即如果 @Data 用作输入(将 OUTPUT 替换为 READONLY)。

4

0 回答 0