Let's say I have a table called TBPedido wich has foreigns keys of other tables:TBPratoPrincipal,TBBebida and TBAperitivo. Each of three tables have a codPreco field that stores the price of a product.
In TBPedido I want to calculate the final price by adding the codPreco. I did a trigger, but it does not work:
Code:CREATE TRIGGER triggerTriste On dbo.TBPedido after INSERT,UPDATE As Declare @total Decimal(7,2),@cod2 Int ,@cod3 Int,@cod4 Int SELECT @cod2=codPrato,@cod3=codBebida,@cod4=codAperitivo FROM inserted If (@cod2 Is Not Null) Begin SET @total= @total +(SELECT codPreco FROM TBPratoPrincipal WHERE idPrato =@cod2) End If (@cod3 Is Not Null) Begin SET @total= @total +(SELECT codPreco FROM TBBebida WHERE idBebida = @cod3) End If (@cod4 Is Not Null) Begin SET @total= @total +(SELECT codPreco FROM TBAperitivo WHERE idAperitivo = @cod4) End UPDATE P SET precoTotal= @total FROM TBPedido P INNER JOIN inserted D On P.idNumeroPedido= D.idNumeroPedido GO
It was supposed to be sql server thing.
Any ideas why is it wrong?
Are you getting any error messages? Also, what is the structure of the tables?
Nope, I don't receive an error. I do:
insert into TBPedido values(1,1,1,1,0);
And where is zero was supossed to become the final price.
And then some inserts, views and the trigger I said. I'm afraid the whole design is wrong, what become this trigger very important.Code:USE BDRestaurante; CREATE TABLE TBPratoPrincipal( idPrato Int Not Null, codNomeItem VarChar(70) Not Null, codPreco Decimal(6,2) Not Null, quantidade Int ); CREATE TABLE TBBebida( idBebida Int Not Null, codNomeItem VarChar(70) Not Null, codPreco Decimal(6,2) Not Null, quantidade Int, alcoolica Int ); CREATE TABLE TBAperitivo( idAperitivo Int Not Null, codNomeItem VarChar(70) Not Null, codPreco Decimal(6,2) Not Null, quantidade Int ); CREATE TABLE TBPedido( idNumeroPedido Int Not Null, codPrato Int, codBebida Int, codAperitivo Int, precoTotal Decimal(7,2) ); CREATE TABLE TBCardapio ( idNomeItem VarChar(70) Not Null, preco Decimal(6,2) Not Null, tipo VarChar(15) Not Null, descricao VarChar(100), CONSTRAINT pkIdNomeItem Primary Key(idNomeItem,preco) ); ALTER TABLE TBPratoPrincipal Add CONSTRAINT pkIdPrato Primary Key(idPrato), CONSTRAINT fkCodNomeItem3 Foreign Key (codNomeItem,codPreco) REFERENCES TBCardapio(idNomeItem,preco) On UPDATE no action On DELETE cascade ALTER TABLE TBBebida Add CONSTRAINT pkIdBebida Primary Key(idBebida), CONSTRAINT fkCodNomeItem Foreign Key(codNomeItem,codPreco) REFERENCES TBCardapio(idNomeItem,preco) On UPDATE no action On DELETE cascade ALTER TABLE TBAperitivo Add CONSTRAINT pkIdAperitivo Primary Key(idAperitivo), CONSTRAINT fkCodNomeItem2 Foreign Key(codNomeItem,codPreco) REFERENCES TBCardapio(idNomeItem,preco) On UPDATE no action On DELETE cascade ALTER TABLE TBPedido Add CONSTRAINT pkIdNumeroItem Primary Key(idNumeroPedido), CONSTRAINT fkCodPrato Foreign Key(codPrato) REFERENCES TBPratoPrincipal (idPrato) On UPDATE no action On DELETE cascade, CONSTRAINT fkCodBebida Foreign Key (codBebida) REFERENCES TBBebida (idBebida) On UPDATE no action On DELETE no action, CONSTRAINT fkCodAperitivo Foreign Key(codAperitivo) REFERENCES TBAperitivo(idAperitivo) On UPDATE no action On DELETE no action
Last edited by asafe; 12-06-2009 at 06:42 AM.
Hahaha I did minor changes and it worked:
Code:CREATE TRIGGER triggerTriste On dbo.TBPedido For INSERT,UPDATE As Declare @total Decimal(7,2),@cod2 Int ,@cod3 Int,@cod4 Int SET @total =0.0 SELECT @cod2=codPrato,@cod3=codBebida,@cod4=codAperitivo FROM inserted If @cod2 Is Not Null SET @total= @total +(SELECT codPreco FROM TBPratoPrincipal WHERE idPrato =@cod2) If @cod3 Is Not Null SET @total= @total +(SELECT codPreco FROM TBBebida WHERE idBebida = @cod3) If @cod4 Is Not Null SET @total= @total +(SELECT codPreco FROM TBAperitivo WHERE idAperitivo = @cod4) UPDATE P SET precoTotal= @total FROM TBPedido P INNER JOIN inserted D On P.idNumeroPedido= D.idNumeroPedido GO
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks