I've been researching how to create a TSQL trigger that will handle multiple Update/Inserts.
We have data coming from multiple sources and my goal is to verify/correct that data before updating/inserting.
I wrote a trigger that works for single rows of data.
I'm struggling to figure out how to get it to handle multiple rows of data.
CREATE TRIGGER [dbo].[tr_GPTitleToGov]
ON [dbo].[GoverningPersons]
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Title1 VARCHAR(15)
DECLARE @UBI VARCHAR(9)
DECLARE @ETPID CHAR(4)
DECLARE @Ident INT
SET @Title1 = (SELECT Title1 FROM INSERTED)
SET @UBI = (SELECT UBI FROM INSERTED)
SET @ETPID = (SELECT [ETPID] FROM [entity] WHERE @UBI = [entity].[UBI])
SET @Ident = (SELECT Ident FROM INSERTED)
IF ((@Title1 = 'Executor') OR (@Title1 = 'Incorporator'))
BEGIN
IF @ETPID IN ('0143', '0147', '0148', '0150', '0152', '0154')
UPDATE GoverningPersons
SET [Title1] = 'Executor',
[Title2] = NULL,
[Title3] = NULL,
[Title4] = NULL
WHERE Ident = @Ident;
ELSE
UPDATE GoverningPersons
SET [Title1] = 'Incorporator',
[Title2] = NULL,
[Title3] = NULL,
[Title4] = NULL
WHERE Ident = @Ident;
END
ELSE
UPDATE GoverningPersons
SET [Title1] = 'Governor',
[Title2] = NULL,
[Title3] = NULL,
[Title4] = NULL
WHERE Ident = @Ident;
END
I think what's throwing me is where to join the fields so I can check the data against data in a different table.
I've never written a trigger before, so any help would be appreciated.