1

我有来自多个表的多个记录。我需要将这些记录插入到我稍后传递的临时表中。问题是当我将行插入临时表时,它插入的数据行意味着每列行 id 计数器都会增加。我需要明智地插入数据列。表示每一列(我从差异表中获取记录)rowId 计数器从 1 开始。如果我在第一列中插入数据,我的行 id 计数器为 1-5(如果有 5 行)。如果我触发了从第 6 行插入到临时表中的第二条语句(对于第 2 列)数据。剩余的(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'   

谢谢!

4

2 回答 2

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'
于 2012-12-17T08:42:27.897 回答
1

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

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

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

于 2012-12-19T15:36:50.980 回答