SQL-SERVER ADD INSERT/MODIFY DATE COLUMN WITH TRIGGER

علی ذوالفقار
1402/04/28 13:09:50 (314)
ALTER TABLE [__TABLE_NAME__] ADD [INSERTDATE] DATETIME2 DEFAULT GETDATE() NULL ; 
ALTER TABLE [__TABLE_NAME__] ADD [MODIFYDATE] DATETIME2 NULL ; 

CREATE TRIGGER [DBO].[TRG___TABLE_NAME___MODIFYDATE]
ON [DBO].[__TABLE_NAME__]
FOR UPDATE
AS
BEGIN
	DECLARE @ID INT 
	SELECT  @ID = ID FROM INSERTED 
	UPDATE  [__TABLE_NAME__] SET [MODIFYDATE] = GETDATE() WHERE ID = @ID 
	-- UPDATE [TABLE_NAME] SET UDATE = GETDATE() WHERE ID IN (SELECT  ID FROM INSERTED )
END
Back