TRIGGER Syntax

Bobby P 231 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
{count} votes

2 answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,431 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


  2. 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

    0 comments No comments

Your answer

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