.net 存储过程,在返回到客户端之前将结果存储在另一个表中

.net 存储过程,在返回到客户端之前将结果存储在另一个表中,.net,sql-server,sql-server-2008,stored-procedures,clrstoredprocedure,.net,Sql Server,Sql Server 2008,Stored Procedures,Clrstoredprocedure,存储过程正在使用下面的查询获取结果并将结果返回给客户端 select @Lid, * from CurrentProductSet cps where cps.State = @state and cps.ProductName in (select gbb.ProductName from HMCGoodBetterBest gbb

存储过程正在使用下面的查询获取结果并将结果返回给客户端

select 
    @Lid, *         
from 
    CurrentProductSet cps 
where 
    cps.State = @state
    and cps.ProductName in (select gbb.ProductName 
                            from HMCGoodBetterBest gbb 
                            where gbb.HMC_Hospital = @hospital 
                              and gbb.HMC_Extras = @extra);
在将结果返回给客户之前,请您指导我如何将这些结果存储在另一个表中以供进一步使用。只是不想两次获取数据或使用表变量。我已经创建了另一个表“Temp\u CurrentProductSet”

编辑:

我尝试在下面的代码中使用into子句,但出现以下错误:

对象或列名丢失或为空。对于SELECT INTO语句,请验证每个列都有名称。对于其他语句,请查找空别名。不允许定义为或[]的别名。将别名更改为有效名称

代码:


正如错误所提示的,您需要为每个列名定义别名

试试这个

insert into Temp_CurrentProductSet
select @Lid, *      
    from CurrentProductSet cps 
    where cps.State=@state
    and 
    cps.ProductName in (select gbb.ProductName from HMCGoodBetterBest gbb where gbb.HMC_Hospital=@hospital and gbb.HMC_Extras=@extra);

问题的关键在于错误:

An object or column name is missing or empty.
您需要为@Lid字段定义一个列名,例如:

select @Lid as Lid, * 
    into Temp_CurrentProductSet
    from ...

请务必意识到,使用SELECT INTO将创建一个新表。如果试图将值插入到现有表中,则需要使用insert into SELECT。

以上查询将结果存储在一个表Temp\U CurrentProductSet中。SELECT INTO就是这样做的。你到底想做什么?如果使用一个表来实现这一点,只需小心并发性等。除非你添加代码,否则数据不会在之后从表中删除,除非你做一些非常棘手的事情,否则运行同一存储过程的两个不同的连接只会使用同一个表,从而产生意外的结果。我建议你提供更多的背景资料,说明你为什么要这样做。是否存在性能问题?在insert中使用output子句显示结果谢谢,但insert into不会返回,我想插入并选择发送给客户端。嗯,不理解?这将创建一个新表,而不是临时表,一个真正的表,然后在存储过程的后面,您可以从该表中进行选择?谢谢,我这样做了,但它仍然无法解决问题。插入将插入但不发送到客户端,我需要做这两件事。
select @Lid as Lid, * 
    into Temp_CurrentProductSet
    from ...
**You need to use output clause**

insert into Temp_CurrentProductSet output Inserted.*
select 
@Lid, *         
from 
CurrentProductSet cps 
where 
cps.State = @state
and cps.ProductName in (select gbb.ProductName 
                        from HMCGoodBetterBest gbb 
                        where gbb.HMC_Hospital = @hospital 
                          and gbb.HMC_Extras = @extra);