在临时表中插入多个表数据

时间:2012-12-17 07:34:27

标签: sql stored-procedures

我有多个表的多个记录。我需要将这些记录插入临时表中,我将在后面传递。问题是当我将行插入临时表时,它插入数据行方式意味着每个列行ID计数器增加。我需要明智地插入数据。每个列的意思(我从diff表中获取记录)rowId计数器从1.if i插入第1列中的数据我的行id计数器是1-5(如果有5行)。如果我从行id 6中激活插入临时表的第二个语句(第二列)数据,剩余的(1-5)是空值。 有什么解决方案吗?请帮我。任何线索都很明显。提前致谢! 以下是我的程序......

alter PROCEDURE procGetPreviousOrderDetails --(@userid nvarchar(100))

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

declare @recentViewedProduct nvarchar(max)
declare @AccountCreatedDate nvarchar(100)
declare @recentViewedCategories nvarchar(max)
declare @recentOrderProductList nvarchar(max)
declare @dateOflastPurchase nvarchar(max)
declare @shippingMethodList nvarchar(max)
declare @paymentMethodList nvarchar(max)
declare @totalOrderAmount nvarchar(100)
declare @zipCode nvarchar(100)

declare @tblOrderDetails table 
(
    RowId int identity(1,1),
    recentViewedProduct nvarchar(max),
    trailType int,
    AccountCreatedDate nvarchar(100),
    recentViewedCategories nvarchar(max),
    recentOrderProductList nvarchar(max),
    dateOflastPurchase nvarchar(max),
    shippingMethodList nvarchar(max),
    paymentMethodList nvarchar(max),
    totalOrderAmount nvarchar(100),
    zipCode nvarchar(100) 

)

if (select count(@@rowcount) from @tblOrderDetails) > 0   
begin                    
    truncate table tblOrderDetails 
end



 insert into  @tblOrderDetails(recentViewedProduct) select RV.item_name, RV.trail_type    from tblCMRecentlyViewed RV where RV.trail_type in (1, 2) and RV.user_id = '807CF717-9413-4CC3-8982-FF63A6B31906' order by RV.date_added desc
 insert into  @tblOrderDetails(AccountCreatedDate) select d_date_registered from UserObject where g_user_id = '807CF717-9413-4CC3-8982-FF63A6B31906'
  select distinct product_id from OrderFormLineItems where ordergroup_id = (select TOP 1 ordergroup_id from Ordergroup where user_id='807CF717-9413-4CC3-8982-FF63A6B31906' 
order by order_create_date desc)
select TOP 1 order_create_date from Ordergroup where user_id='807CF717-9413-4CC3-8982-FF63A6B31906' order by order_create_date desc
select distinct display_name from  ShipMethods where carrier_ship_method_id = (select distinct ship_method_id from Ordergroup where user_id='328239F8-A983-442C-A9E6-ADC203D5F52B')
select distinct saved_payment_type from OrderFormHeader where ordergroup_id in (select distinct ordergroup_id from Ordergroup where user_id='328239F8-A983-442C-A9E6-ADC203D5F52B')
select SUM(saved_cy_total_total) from Ordergroup where user_id='807CF717-9413-4CC3-8982-FF63A6B31906' 
select distinct u_postal_code from Addresses where g_id = '807CF717-9413-4CC3-8982-FF63A6B31906'   

谢谢!

2 个答案:

答案 0 :(得分:1)

我会说你这是错误的。这些查询中的每一个都应该是一个独立的过程。合并数据应该是业务逻辑的责任。

您应该考虑在一个过程中返回AccountCreatedDate,DateOfLastPurchase和TotalOrderAmmount,因为它只是一行,其余的应该有自己的过程。

如果您真的想要这样做,您需要在每个子选择上执行LEFT JOIN并在单个查询中执行所有操作。但表现会受到影响:

insert into @tblOrderDetails
select u.user_id, u.g_date_registered 
from UserObject u 
left join RV on u.user_id = RV.user_id 
    and RV.trail_type in (1, 2)
left join (
    select user_id, product_id 
    from OrderFormLineItems 
    where ordergroup_id = (
        select TOP 1 ordergroup_id 
        from Ordergroup 
        where user_id='807CF717-9413-4CC3-8982-FF63A6B31906')   
    ) ofli on ofli.user_id = u.user_id
left join (
    select TOP 1 user_id, order_create_date 
    from Ordergroup 
    where user_id='807CF717-9413-4CC3-8982-FF63A6B31906' 
    order by order_create_date desc
    ) og on og.user_id = u.user_id
left join (
    select distinct user_id, display_name 
    from  ShipMethods 
    where carrier_ship_method_id = (
        select distinct ship_method_id 
        from Ordergroup 
        where user_id='328239F8-A983-442C-A9E6-ADC203D5F52B')
    ) sm on sm.user_id = u.user_id
-- etc ...
where u.user_id = '328239F8-A983-442C-A9E6-ADC203D5F52B'

答案 1 :(得分:1)

使用第一个INSERT插入行后,必须更新行以更改行中的其他列。这意味着您的临时表必须包含每行的相关标识信息,以便您可以准确地应用更新。

或者,按照Patrik Melander的建议,将SELECT语句合并为一个;这意味着您只能插入完整的记录。

这两种机制都可以避免您遇到的麻烦。