我在訂單表上有一個觸發器,它將訂單詳細信息插入到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
使用'INNER JOIN'的意義就在於能夠定義**加盟條件**的JOIN操作之後 - 所以它應該是' INNER JOIN dbo.Vendor_Contacts ON ...(在此處定義您的條件)....' – 2013-02-20 20:48:10