Jump to content

Triggers Sql Server 2008


Damon4hire
 Share

Recommended Posts

boas

Estou a ter problemas com os triggers da minha BD,

não é matéria que domine lol.

Tenho estas tabelas:

Dvds

------

id - prim key (identity)

.....etc.......

quantidade (int)

EntradasDetalhes

-----------------------

id - prim key (identity)

.....etc.......

id_dvd (int)

quantidade_recebida (int)

A ideia é cada vez que for um registo introduzido na tabela [EntradasDetalhes]

ele devia fazer

Dvds.quantidade = Dvds.quantidade + [EntradasDetalhes].quantidade_recebida

Cheguei aqui e fiquei:

create trigger aumentaStock on EntradasDetalhes

after insert as

update Dvds

......

uma ajuda por favor

Link to comment
Share on other sites

Faz algo tipo isto:

CREATE TRIGGER aumentaStock ON EntradaDetalhes
AFTER INSERT
AS
DECLARE @quant INTEGER
SELECT @quant = (SELECT quantidade_recebida FROM EntradasDetalhes)
UPDATE Dvds SET quantidade = (SELECT quantidade FROM Dvds) + @quant

Não sei se isto funciona, mas é só para veres mais ou menos a lógica, vais buscar o novo valor, guarda-lo e depois actualizas a tabela Dvds. Agora tens de ver como se faz isto em SQL Server.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.