简体   繁体   中英

sql trigger after insert,update same table

My data is imported through the external system into the SQL Server. I want to write a trigger that allows all data to be added to the database by default unless they have special conditions to be updated (they must be updated).

I mean, we should search on all input records and if we have some duplicate data (based on the above conditions), the insert operation should not be performed and only the new input data should replace the old data (Update Action).

Could you please help me?

Try this

CREATE TRIGGER TR_YourTable ON dbo.YourTable
FOR INSERT
AS
SET NOCOUNT ON;
DELETE tbl
FROM dbo.YourTable AS tbl
JOIN inserted ON inserted.ID = tbl.ID
WHERE inserted.ID in (select ID from dbo.YourTable);
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM