将存储过程和其他变量插入临时表

时间:2022-11-20 16:42:03

I have a stored procedure and 3 variables. I need to insert them all on the same line in a temp table. Is this possible? I tried

我有一个存储过程和3个变量。我需要将它们都插入到临时表的同一行中。这是可能的吗?我试着

INSERT INTO #Temp (@Ad, @B, @C, @SPInt) 
    SELECT 
        @Ad, @B, @C, EXEC sp_XYZ @A, @B, @C

But this does not work.

但这行不通。

2 个解决方案

#1


3  

This simplest approach is to modify the stored procedure to return the input arguments along with everything else.

这种最简单的方法是修改存储过程,以返回输入参数和其他所有内容。

Absent that, you need to store the output results somewhere and then insert into #Temp. Here is one way:

否则,您需要将输出结果存储在某处,然后插入到#Temp中。这是一种方法:

create table #temp_exec (. . .);
insert into #temp_exec ( . . . )
    exec sp_xyz @A, @B, @C;

insert into #temp (a, b, c, . . . )
    select @A, @B, @C, . . .
    from #temp_exec;

If #temp starts out as empty, then you don't need another table:

如果#temp一开始是空的,那么您不需要另一个表:

insert into #temp ( . . . )
    exec sp_xyz @A, @B, @C;

update #temp
    set a = @A, b = @b, c = @c;

#2


1  

No you cannot do this all in one statement.

不,你不可能在一种说法里做到这一切。

You can do the INSERT..EXEC in one statement, and then add the variables in an UPDATE statement. But you have to use two different statements to do this.

你可以插入。在一条语句中执行,然后在一条更新语句中添加变量。但是你必须用两个不同的表述来做这个。

#1


3  

This simplest approach is to modify the stored procedure to return the input arguments along with everything else.

这种最简单的方法是修改存储过程,以返回输入参数和其他所有内容。

Absent that, you need to store the output results somewhere and then insert into #Temp. Here is one way:

否则,您需要将输出结果存储在某处,然后插入到#Temp中。这是一种方法:

create table #temp_exec (. . .);
insert into #temp_exec ( . . . )
    exec sp_xyz @A, @B, @C;

insert into #temp (a, b, c, . . . )
    select @A, @B, @C, . . .
    from #temp_exec;

If #temp starts out as empty, then you don't need another table:

如果#temp一开始是空的,那么您不需要另一个表:

insert into #temp ( . . . )
    exec sp_xyz @A, @B, @C;

update #temp
    set a = @A, b = @b, c = @c;

#2


1  

No you cannot do this all in one statement.

不,你不可能在一种说法里做到这一切。

You can do the INSERT..EXEC in one statement, and then add the variables in an UPDATE statement. But you have to use two different statements to do this.

你可以插入。在一条语句中执行,然后在一条更新语句中添加变量。但是你必须用两个不同的表述来做这个。