sql - How to set that one record is marked 1 and all other 0 -
i have table addresses
columns id (pk), empid (int), address (nvarchar(100), isdefault (bit)
. have ensure 1 record same empid have isdefault set 1. have done following triggers. first 1 insert trigger. first it's checks if there entered value 1 in isdefault
. if yes, checks if there more 1 record same empid
, isdefault
value set on 1. if true, sets other isdefault
values same empid
0. :
create trigger [dbo].[trg_dbo_addresses_isdefault_onlyonerecord_insert] on [dbo].[laddressesorganisations] after insert begin set nocount on; begin try if exists ( select * inserted i.isdefault = 1) begin if ( select count(*) dbo.addresses lao inner join inserted on i.id=lao.id lao.isdefault = 1 , lao.empid = i.empid ) > 1 begin update lao set lao.isdefault = 0 dbo.addresses lao ( select row_number () on (partition empid order id desc) rn dbo.addresses lao ) > 1 end end end try begin catch if @@trancount > 0 rollback tran; end catch end
this other 1 update trigger don't have clue how write it. first same insert trigger, checking inserted value 1 value 0. if yes, if there more 1 record same empid
, isdefault
set 1. if yes,
how write other records set 0 same empid
, while 1 being updated remain 1?
create trigger [dbo].[trg_dbo_addresses_isdefault_onlyonerecord_update] on [dbo].[addresses] after update begin set nocount on; begin try if exists ( select * inserted inner join deleted d on d.id=i.id i.isdefault = 1 , d.isdefault = 0) begin if ( select count(*) dbo.addresses lao inner join inserted on i.id=lao.id lao.isdefault = 1 , lao.empid = i.empid ) > 1 begin update lao set lao.isdefault = 0 dbo.addresses lao inner join inserted on i.id=lao.id (i don't have idea put here) , lao.organisationid = i.organisationid end end end try begin catch if @@trancount > 0 rollback tran; end catch end
i'm working on ms sql 2016.
i agree matt: if possible, avoid trigger.
anyway, think in update trigger should change to:
if ( select count(*) dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 ) > 1 begin update lao set lao.isdefault = 0 dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 , lao.id <> i.id end
morevore, can rewrite to:
if exists(select 1 dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 , lao.id <>i.id) begin update lao set lao.isdefault = 0 dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 , lao.id <> i.id end
you should change insert trigger too.
updated: insert trigger. far can see (but can't tests, please complete case tests), , if id max value or if want preserve default of last id inserted, think rewrite insert trigger following: (you remove if too, if don't care doing update 0 rows too)
as begin set nocount on; begin try if exists(select 1 dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 , lao.id <>i.id , i.isdefault=1) begin update lao set lao.isdefault = 0 dbo.addresses lao inner join inserted on lao.empid = i.empid lao.isdefault = 1 , lao.id <> i.id , i.isdefault=1 end end try begin catch if @@trancount > 0 rollback tran; end catch
this change (and i.isdefault=1) applied update trigger too.
Comments
Post a Comment