I have stuck on this trigger when I use to select case from the inserted table, the result was NULL.
TRIGGER trgInsertPenjDetail ON [dbo].[TbDtlOutBrgGd1]
AFTER INSERT
AS
BEGIN
DECLARE @namaProduct varchar (255)
DECLARE @jenisProduct varchar (50)
SET @jenisProduct = (select jenis from Inserted)
SELECT @namaProduct =
CASE @jenisProduct
WHEN 'PAKET'
THEN (SELECT tb.nm AS namaProduct from dbo.TbHdPaket AS tb
INNER JOIN Inserted AS i ON tb.id = i.brg)
WHEN 'TERAPI'
THEN (SELECT tb.nm AS namaProduct from dbo.TbMterapi AS tb
INNER JOIN Inserted AS i ON tb.id = i.brg)
WHEN 'BARANG'
THEN (SELECT tb.nama AS namaProduct from dbo.TbMstBb AS tb
INNER JOIN Inserted AS i ON tb.id = i.brg)
ELSE '-'
END
BEGIN
UPDATE b
SET b.rek = b.rek + '( ' + convert(varchar(5),i.qty) + ' ' + @namaProduct+' ' + i.ket+ ' )'
FROM dbo.TbRek AS b
INNER JOIN Inserted AS i ON b.nott = i.nott
END
BEGIN
UPDATE b
SET b.rek = replace(b.rek, ')(', '+')
FROM dbo.TbRek AS b
INNER JOIN Inserted AS i ON b.nott = i.nott
END
END
What is the right syntax for CASE or IF on this trigger? Thanks.
I would change the top part of the trigger so that the trigger looks like this:
This way even with multiple inserts the inserted table should update TbRek with everything that gets updated.
I assume the bottom part of the query is just to make sure that “)(” gets replaced with a “+”.