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



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

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_CurrentProductSet"。

编辑:

我尝试使用into子句尝试下面的代码,但我得到了这个错误:

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

代码:

select 
    @Lid, * 
into 
    Temp_CurrentProductSet
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

**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);

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

试试这个吧,

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);

最新更新