Share via

TRIGGER Syntax

Bobby P 271 Reputation points
2023-04-24T15:24:04.37+00:00

Looking to create a TRIGGER on INSERT of a New Row. Is this correct? Do I need to qualify it? IntelliSense is kind of asking for a WHERE Clause...

CREATE TRIGGER [TR_rsm_emailnotification_INSERT]
ON [dbo].[rsm_emailnotification]
AFTER INSERT
AS
	BEGIN
		SET NOCOUNT ON
		;
		UPDATE		[dbo].[rsm_emailnotification]
		SET			[SinkCreatedOn] = GETDATE (),
					[SinkModifiedOn] = GETDATE (),
					[rsm_notificationtype_entitytype] = 'rsm_status',
					[rsm_program_entitytype] = 'po_program',
					[regardingobjectid_entitytype] = 'rsm_enrollment',
					[createdby_entitytype] = 'systemuser',
					[modifiedby_entitytype] = 'systemuser',
					[owninguser_entitytype] = 'systemuser',
					[owningbusinessunit_entitytype] = 'businessunit',
					[rsm_notificationstatus_entitytype] = 'rsm_status',
					[ownerid_entitytype] = 'systemuser'
		FROM		[dbo].[rsm_emailnotification]
		;
	END
	;
GO
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


2 answers

Sort by: Most helpful
  1. Anonymous
    2023-04-25T01:47:53.31+00:00

    Hi @Bobby P

    As Naomi and Erland say, you need to use virtual tables in triggers.

    You can refer to this post.

    https://www.mssqltips.com/sqlservertip/7429/sql-triggers-for-inserts-updates-and-deletes-on-a-table/

    Best regards,

    Percy Tang

    Was this answer helpful?

    0 comments No comments

  2. Naomi Nosonovsky 8,906 Reputation points
    2023-04-24T15:55:03.4433333+00:00

    You need to INNER JOIN with inserted on the Primary key so you would only update the newly inserted rows and not the whole table

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.