sql - How to ignore unique Index on multiple columns based on column value while inserting new record -
we have table 3 columns, studentid
, subjectid
, active
(and few other columns not related question).
active
column indicates whether record active or not(we set active
column 0 if deletes records ui)
definition of index on columns studentid
, subjectid
below:
create unique nonclustered index [uq_studentsubject_subjectid_studentid] on [dbo].[studentsubject] ( [studentid] asc, [subjectid] asc )with( pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, drop_existing = off, online = off, allow_row_locks = on, allow_page_locks = on ) on [primary] go
from our application, if try insert record combination of subjectid
, studentid
insert failing , java throwing below error:
caused by: com.microsoft.sqlserver.jdbc.sqlserverexception: cannot insert duplicate key row in object 'dbo.studentsubject' unique index 'uq_studentsubject_subjectid_studentid'. duplicate key value (113460, 182).
the record combination 113460, 182 having active 0 so, trying insert new record instead of setting active flag 1.
is there way can ignore index while inserting if insert new record existing combination of subjectid , studentid have active
column zero?
edit sorry confusion, index not constraint.
below example of filtered index ignore rows active value 0:
create unique nonclustered index uq_studentsubject_subjectid_studentid on dbo.studentsubject ( [studentid], [subjectid] ) active <> 0;
Comments
Post a Comment