Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

sql insert trigger - update duplicate row

$
0
0

I have to update a duplicate row. This is a new table so there'll be no duplicates but if someone tries to insert a duplicate I need to update the duplicate row with the new values instead of creating a new one. This is what I have for triggering the trigger

SQL
CREATETRIGGER<name>ON<table>AFTERINSERTASIF(SELECTcount(ID)frominsertedWHEREEXISTS(SELECTIDFROM<table>WHERE<table>.<col1>=inserted.<col1>ANDID!=inserted.ID))>0BEGINUPDATE<table>SET<col1>=<somevalue>,<col2>=<somevalue>WHEREID=<somethinggoeshere)END

Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>