I have a table named dbo.Inventory
in SQL Server 2016 like this:
ProdCode UPCCode
1 11111
2 11112
3 11113
When adding a new row, I need to write a trigger that will check for a duplicate in that column. If it is a duplicate I need it to give an error and not change the column. Otherwise, I need it to insert like normal.
I can't use a unique index because I have a lot of rows with a value of NULL.
Best Answer
I'd stay away from triggers as much as you can, cause they're generally pretty bad for performance. I'd suggest using a non clustered index with a filter.
See below example
That should give you the requested results, without needing to use a trigger.