c# - How can i update header table Status as Closed comparing with detail table Status in SQL -
header
table:
headeid status ---------------- 1 open
detail
table:
detailid headerid status ------------------------- 1 1 close 2 1 close
sql server allows use of triggers. in simple terms, trigger stored procedure executes on update of table. typically use triggers enforce business rules/logic.
therefore apply situation place trigger on details table, check if rows current headerid have been set closed update header table.
create trigger detail_update_header on detail after insert, update begin set nocount on; declare @currentheaderid int; select @currentheaderid = i.headeid inserted i; if((select count(*) detail headerid = @currentheaderid) > (select count(*) detail headerid = @currentheaderid , status = 'closed')) begin update header set status = 'closed'; end end go
Comments
Post a Comment