2013-02-20 48 views
2

我在訂單表上有一個觸發器,它將訂單詳細信息插入到order_details表中。這僅在輸入一行時有效,但當多個行一次插入時不適用。我已經閱讀了多個關於使用遊標的網站上的多個線程,而語句,臨時表等我嘗試了一些但沒有成功。任何關於最佳/最簡單的方式的建議,以確保在下訂單時添加所有詳細的行。使用插入觸發器處理多行

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER TRIGGER [dbo].[CreateOrderDetails] 
ON [dbo].[Orders] 
FOR INSERT 
AS 
    declare @OrderStagingId uniqueidentifier 
    declare @OrderType as varchar(50) 
    declare @OrderID uniqueidentifier 
    declare @LocationID uniqueidentifier 
    declare @status as varchar (25) 
    declare @vendorid as uniqueidentifier 

    SELECT @OrderID = OrderID, @LocationID = LocationID, 
      @vendorid = vendorid, @OrderStagingId = OrderStagingId 
    FROM inserted 

    Begin 

    SET NOCOUNT ON; 

    -- Insert statements for trigger here 

    Insert into [Brain].dbo.[Order_Details] 

    SELECT  
     NEWID() AS OrderDetailId, @OrderID AS OrderId, 
     dbo.OrderStaging_Details.EcProductID,  
     dbo.OrderStaging_Details.Qty, dbo.OrderStaging_Details.Qty_Type, 
     dbo.OrderStaging_Details.Cost, dbo.OrderStaging_Details.Ext_Cost, 
     dbo.OrderStaging_Details.EnteredBy, 
     NULL AS ReceivedBy, NULL AS ReceivedDate, NULL AS ReceivedQty, 
     dbo.OrderStaging_Details.OrderNote, NULL AS ReceivedNote,  
     dbo.OrderStaging_Details.UpdatedBy, dbo.OrderStaging_Details.UpdateDate 
    FROM   
     dbo.Vendor_Assigned_Locations 
    INNER JOIN 
     dbo.Vendor_Contacts 
    INNER JOIN 
     dbo.Vendors 
    INNER JOIN 
     dbo.OrdersStaging 
    INNER JOIN 
     dbo.OrderStaging_Details ON dbo.OrdersStaging.OrderStagingID = dbo.OrderStaging_Details.OrderStagingID 
      ON dbo.Vendors.VendorID = dbo.OrderStaging_Details.VendorId 
      ON dbo.Vendor_Contacts.Vendor_ID = dbo.Vendors.VendorID 
      ON dbo.Vendor_Assigned_Locations.LocationID = dbo.OrdersStaging.LocationID 
       AND dbo.Vendor_Assigned_Locations.VCID = dbo.Vendor_Contacts.VCID 
    INNER JOIN 
     dbo.Orders ON dbo.OrdersStaging.OrderStagingID = dbo.Orders.OrderStagingID 
        AND dbo.Vendors.VendorID = dbo.Orders.VendorID 
        AND dbo.Vendor_Contacts.VCID = dbo.Orders.VendorContactID 
        AND dbo.OrdersStaging.LocationID = dbo.Orders.LocationID 
    LEFT OUTER JOIN 
     dbo.Order_Details ON dbo.Orders.OrderID = dbo.Order_Details.OrderID 
    WHERE  
     (dbo.OrderStaging_Details.OrderStagingID = @OrderStagingID) 
     AND (dbo.OrdersStaging.LocationID = @Locationid)  
     AND (dbo.Vendors.VendorID = @Vendorid) 
     AND (dbo.Order_Details.OrderID IS NULL) 
    end 
+0

使用'INNER JOIN'的意義就在於能夠定義**加盟條件**的JOIN操作之後 - 所以它應該是' INNER JOIN dbo.Vendor_Contacts ON ...(在此處定義您的條件)....' – 2013-02-20 20:48:10

回答

0

這觸發可能是必要的,你

ALTER TRIGGER [dbo].[CreateOrderDetails] ON [dbo].[Orders] 
FOR INSERT 
AS 
BEGIN 
    SET NOCOUNT ON;  
    -- Insert statements for trigger here  
    INSERT INTO [Brain].dbo.[Order_Details]  
    SELECT  
    NEWID() AS OrderDetailId, i.OrderID AS OrderId, 
    dbo.OrderStaging_Details.EcProductID,  
    dbo.OrderStaging_Details.Qty, dbo.OrderStaging_Details.Qty_Type, 
    dbo.OrderStaging_Details.Cost, dbo.OrderStaging_Details.Ext_Cost, 
    dbo.OrderStaging_Details.EnteredBy, 
    NULL AS ReceivedBy, NULL AS ReceivedDate, NULL AS ReceivedQty, 
    dbo.OrderStaging_Details.OrderNote, NULL AS ReceivedNote,  
    dbo.OrderStaging_Details.UpdatedBy, dbo.OrderStaging_Details.UpdateDate 
    FROM    
    dbo.OrdersStaging 
    INNER JOIN 
    dbo.OrderStaging_Details ON dbo.OrdersStaging.OrderStagingID = dbo.OrderStaging_Details.OrderStagingID 
    INNER JOIN 
    dbo.Vendors ON dbo.Vendors.VendorID = dbo.OrderStaging_Details.VendorId 
    INNER JOIN 
    dbo.Vendor_Contacts ON dbo.Vendor_Contacts.Vendor_ID = dbo.Vendors.VendorID 
    INNER JOIN   
    dbo.Vendor_Assigned_Locations ON dbo.Vendor_Assigned_Locations.LocationID = dbo.OrdersStaging.LocationID 
             AND dbo.Vendor_Assigned_Locations.VCID = dbo.Vendor_Contacts.VCID 
    INNER JOIN 
    dbo.Orders ON dbo.OrdersStaging.OrderStagingID = dbo.Orders.OrderStagingID 
        AND dbo.Vendors.VendorID = dbo.Orders.VendorID 
        AND dbo.Vendor_Contacts.VCID = dbo.Orders.VendorContactID 
        AND dbo.OrdersStaging.LocationID = dbo.Orders.LocationID 
    INNER JOIN 
    inserted i ON dbo.Orders.OrderID = i.OrderID 
END 
+0

似乎正在與此合作。感謝你的協助! – 2013-02-21 16:08:31

+0

沒問題:)... – 2013-02-22 09:04:33

0

你在哪裏撿@OrderID,@LocationID等外部select語句,只選擇第一排出來的插入其中包含了所有被插入在扣動了扳機交易的訂單。

Use insert into detail(...) 
Select NewID(), Inserted.OrderID, ... 
From Inserted 
inner join ... 
Where etc 

改爲。

+0

感謝您的協助。這正是它所做的。在使用Alexanders例子後,我能夠使其工作。每個回覆的人都有同樣的建議,並且所有人都是正確的。 – 2013-02-21 16:12:11

0

這是因爲你在考慮行而不是列。轉儲@variables,並加入inserted

+0

謝謝我這樣做,因爲亞歷山大在他的代碼的修改版本中顯示,它似乎現在工作。再次感謝您的幫助 – 2013-02-21 16:10:03