0
I have a table called Consumption, in this table I have a column Quantity. I have another table called Product, in this table I have a column Stock.
Where a new consumption is introduced, a product and the quantity consumed of that product shall be chosen.
I need to make a Rigger where each consumption made decreases the stock of the product chosen according to the quantity informed. Example: Product 1 has 10 in stock. Product Consumption 1, 2 quantities. Update Product Stock 1 to 8.
I did not reach the conclusion of any code. Thank you.
What data are you working with? Mysql?
– Sveen
Search for Trigger
– Sveen
I am using Sql Server
– Pedro Henrique Rocha
one
trigger
does not guarantee that the value will even be updated, it seems to me a transactional process, already thought to update everything within atransaction
to ensure?– Ricardo Pontual
Which Server are you working with? C#?
– Sveen
I never saw transaction
– Pedro Henrique Rocha
I am working with C#, but I need this in Sql Server
– Pedro Henrique Rocha
Either you work with Trigger, or you implement the solution in C#, or you have no alternative.
– Sveen
right in this situation would be Rigger, maaaaaaas, makes an expensive stock table... greatly simplifies
– Rovann Linhalis
table:
movimento_estoque
...produto | valor | quantidade | saldo_anterior | saldo_atual
in this table only makes Inserts, no update. So you keep all drive history;– Rovann Linhalis
I managed to solve with Trigger, I will post the code
– Pedro Henrique Rocha
@Ricardopunctual, I think by default triggers have an implicit transaction. If there is an error in the update, Insert would also not be executed
– Diego Rafael Souza
Interesting @Diegorafaelsouza I will give a search, thanks
– Ricardo Pontual