Help explain this trigger, how do I not understand? ?

xiaoxiao2021-03-06  32

There is a column in my SQL Server data table that allows data to be empty (NULL). I hope that when the data in this column is non-null, this data is unique. What is the best way to program this to achieve this? If I can place a unique constraint on the column, I can only contain a null value in this column. I am using a trigger to strengthen this limit, but can you recommend a simpler way to make sure all non-empty values ​​is unique? Use Tempdb Create Table T1 (C1 Int Null, C2 Char (5) NULL) Create Trigger MyTrigger on T1 for Insert, Update AS Begin IF (SELECT MAX (I.C1) AS CNT from T1, Inserted I where t1.c1 = i.c1 group by i.c1) x)> 1 rollback TRAN END hopes that the master can explain a few code that is painted red. Thank you!

转载请注明原文地址:https://www.9cbs.com/read-64276.html

New Post(0)