USE [cT]
GO
/****** Object: Trigger [dbo].[testinsert] Script Date: 5/4/2018 12:13:36 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[testinsert] ON [dbo].[InvMovements]
/** for iserted lines only **/
AFTER insert
AS BEGIN
declare @TrnType char(1)
declare @Source char(1)
declare @MovementType char(1)
/** check if transaction is originated from PO **/
SELECT @TrnType = inserted.TrnType, @Source = inserted.Source, @MovementType = inserted.MovementType from inserted
if ((@TrnType = 'R') and (@Source = 'P') and (@MovementType = 'I'))
Here I need to check the entry in lot detail table.
Begin
declare @RandomNumber VARCHAR(4) =
RIGHT('0000'+CONVERT(varchar(4),convert(int,rand()*10000)),4)
UPDATE dbo.InvMovements
SET LotSerial = dbo.InvMovements.LotSerial + '.' + @RandomNumber
FROM inserted
WHERE
dbo.InvMovements.StockCode = inserted.StockCode and
dbo.InvMovements.Warehouse = inserted.Warehouse and
dbo.InvMovements.TrnYear = inserted.TrnYear and
dbo.InvMovements.TrnMonth = inserted.TrnMonth and
dbo.InvMovements.EntryDate = inserted.EntryDate and
dbo.InvMovements.LotSerial = inserted.LotSerial and
dbo.InvMovements.Bin = inserted.Bin
UPDATE dbo.LotTransactions
/** append this number to existing lot number following a period **/
SET Lot = dbo.LotTransactions.Lot + '.' + @RandomNumber
FROM inserted
WHERE
dbo.LotTransactions.StockCode = inserted.StockCode and
dbo.LotTransactions.Warehouse = inserted.Warehouse and
dbo.LotTransactions.JnlYear = inserted.TrnYear and
dbo.LotTransactions.JnlMonth = inserted.TrnMonth and
dbo.LotTransactions.Lot = inserted.LotSerial and
dbo.LotTransactions.Bin = inserted.Bin and
dbo.LotTransactions.JobPurchOrder = inserted.Job and
dbo.LotTransactions.Journal = inserted.Journal and
dbo.LotTransactions.JournalLine = inserted.JournalEntry
END
END
Before I perform any updates in Inventory Movement I need to check a record in Lot Transaction Table.
dbo.LotTransactions.StockCode = inserted.StockCode and
dbo.LotTransactions.Warehouse = inserted.Warehouse and
dbo.LotTransactions.JnlYear = inserted.TrnYear and
dbo.LotTransactions.JnlMonth = inserted.TrnMonth and
dbo.LotTransactions.Lot = inserted.LotSerial and
dbo.LotTransactions.Bin = inserted.Bin and
dbo.LotTransactions.JobPurchOrder = inserted.Job and
dbo.LotTransactions.Journal = inserted.Journal and
dbo.LotTransactions.JournalLine = inserted.JournalEntry and dbo.LotTransaction.Line = 1
If the line is 1 I can proceed and follow with my code to update InvMovements and Lot Transaction Table.
How can I check for this first?
Thanks