sql server - decrement a column from a table while decrement a column from another table -
i have 2 tables aprovizionari (which means supplies- of books) , facturi (which means invoices). tables have following structure:
create table aprovizionari ( id_aprovizionare int identity(1,1) primary key, codcarte char(3) foreign key references carti(codcarte), codlibrarie char(3) foreign key references librarii(codlibrarie), dataaprovizionare date default getdate(), cantitate int default 1 -- ^^^^^^^ should decrement when cantitate facturi increment ) create table facturi ( codfactura char(3) primary key, codclient char(4) foreign key references clienti(codclient), codcarte char(3) foreign key references carti(codcarte), cantitate int default 1, -- ^^^^^^^ value here must decrement in aprovizionari.cantitate codlibrarie char(3) foreign key references librarii(codlibrarie), )
aprovizionari
(supply) stores cantitate
(quantity) of each book in stock.
each record in facturi
(invoices) stores purchase of quantity cantitate
of particular book.
books can identified codcarte
in both tables.
everytime invoice recorded in facturi
, quantity of books invoiced should subtracted quantity of book available in aprovizionari
. how can achieved?
if don't insert facturi
in many different places should quite easy perform update on aprovizionari
after insert
, e.g.
insert facturi (codcarte, cantitate, ...) values (101, 2); update aprovizionari set cantitate = cantitate - 2 codcarte = 101;
otherwise trigger along lines of:
create trigger facturi_oninsert on facturi after insert begin update set a.cantitate = a.cantitate - i.cantitate aprovizionari inner join inserted on i.codcarte = a.codcarte end
Comments
Post a Comment