我不确定如何打开标题,但让我解释一下我的问题。
我有两个表,它们都使用uniqueidentifier作为Id,它们是由newsequentialid()
自动生成的现在,当我对表B进行插入时,它符文插入触发器,我在此触发器中执行一些特定的操作,并且还将一些值插入到另一个名为A的表中,我需要检索此表A' s插入Id但我无法找到解决方案。
还让我解释为什么我需要这样的触发器。当用户在其中创建包含库存信息的产品的发票时,此触发器负责创建库存交易,其中包含有关所插入产品的标题和详细信息(此库存明细表还具有触发器并更新仓库)等。等。
我希望这能提示我正在尝试做什么
CREATE TRIGGER [dbo].[IT_TBLDebitInvoiceDetails] ON [dbo].[TBLDebitInvoiceDetails] AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
declare @cnt int;
--HEADER
declare @DocumentId uniqueidentifier;
declare @OrganizationId uniqueidentifier;
declare @Date date;
declare @TermDate date;
declare @DespatchDate date;
declare @DespatchNo nvarchar(20);
declare @WarehouseId uniqueidentifier;
declare @CustomerId uniqueidentifier;
declare @StockId uniqueidentifier;
declare @CurrencyTypeId uniqueidentifier;
declare @GrandTotal money;
--Auditable
declare @WhoCreated uniqueidentifier;
declare @DateCreated datetime;
declare @WhoUpdated uniqueidentifier;
declare @DateUpdated datetime;
--DETAIL
declare @ProductId uniqueidentifier;
declare @Quantity decimal(18, 2);
declare @ProductType int;
SELECT TOP(1) @OrganizationId = OrganizationId, @DocumentId = A.Id, @Date = A.[Date], @TermDate = A.TermDate, @DespatchDate = A.DespatchDate, @DespatchNo = A.DespatchNo,
@WarehouseId = A.WarehouseId, @CustomerId = A.CustomerId, @GrandTotal = A.GrandTotal,
@WhoCreated = A.WhoCreated, @DateCreated = A.DateCreated, @WhoUpdated = A.WhoUpdated, @DateUpdated = A.DateUpdated
FROM TBLDebitInvoices AS A
INNER JOIN inserted AS B ON A.Id = B.InvoiceId
/* CHECK STOCK TRANSACTION */
SELECT @cnt = COUNT(*) FROM inserted AS A
INNER JOIN TBLProducts AS B ON B.Id = A.ProductId
WHERE B.ProductType != 1
--we have products for stock, create stock header
IF(@cnt > 0)
BEGIN
INSERT INTO TBLStocks (OrganizationId, TransactionType, DocumentType, DocumentId, [Date], DeliveryDate, DeliveryNo, SourceWarehouseId, CustomerId, [Description], WhoCreated, DateCreated, WhoUpdated, DateUpdated, IsDeleted)
VALUES (@OrganizationId, 5, 0, @DocumentId, @Date, @DespatchDate, @DespatchNo, @WarehouseId, @CustomerId, '', @WhoCreated, @DateCreated, @WhoUpdated, @DateUpdated, 0);
SELECT @StockId = ???????;
END
INSERT INTO TBLStockDetails (StockId, ProductId, [Value])
SELECT @StockId, ProductId, SUM(Quantity) FROM (
SELECT A.ProductId AS ProductId, A.Quantity FROM inserted AS A
INNER JOIN TBLProducts AS B ON B.Id = A.ProductId
WHERE B.ProductType = 0
UNION ALL
SELECT C.IngredientID, A.Quantity * C.Quantity FROM inserted AS A
INNER JOIN TBLProducts AS B ON B.Id = A.ProductId
INNER JOIN TBLProductRecipes AS C ON C.ProductId = B.Id
WHERE B.ProductType = 2
) AS T1
GROUP BY ProductId;
UPDATE TBLDebitInvoices SET StockId = @StockId WHERE Id = @DocumentId;
/* CHECK DC TRANSACTION */
INSERT INTO TBLDebitCreditTransactions (TransactionType, DocumentType, DocumentId, PaymentStatus, [Date], Amount, AccountType, AccountId, CurrencyTypeId)
VALUES (1, 0, @DocumentId, 0, @TermDate, @GrandTotal, 0, @CustomerId, @CurrencyTypeId);
END
GO
在这个触发器中我有这个插入:
INSERT INTO TBLStocks (OrganizationId, TransactionType, DocumentType, DocumentId, [Date], DeliveryDate, DeliveryNo, SourceWarehouseId, CustomerId, [Description], WhoCreated, DateCreated, WhoUpdated, DateUpdated, IsDeleted)
VALUES (@OrganizationId, 5, 0, @DocumentId, @Date, @DespatchDate, @DespatchNo, @WarehouseId, @CustomerId, '', @WhoCreated, @DateCreated, @WhoUpdated, @DateUpdated, 0);
SELECT @StockId = ???????;
我需要将Id插入此表,以便我可以使用其id插入其行元素。
答案 0 :(得分:0)
似乎我找到了答案,我不确定插入语句会为第二个插入提供单独的结果,但它可以正常工作
DECLARE @IdTable TABLE (StockId uniqueidentifier);
INSERT INTO TBLStocks (OrganizationId, TransactionType, DocumentType, DocumentId, [Date], DeliveryDate, DeliveryNo, SourceWarehouseId, CustomerId, [Description], WhoCreated, DateCreated, WhoUpdated, DateUpdated, IsDeleted)
OUTPUT Inserted.Id INTO @IdTable(StockId)
SELECT @OrganizationId, 5, 0, @DocumentId, @Date, @DespatchDate, @DespatchNo, @WarehouseId, @CustomerId, '', @WhoCreated, @DateCreated, @WhoUpdated, @DateUpdated, 0;
SELECT @StockId = StockId FROM @IdTable;
这将为第二个表插入uniqueidentifier