Closed Thread
Results 1 to 6 of 6

Thread: Problem with trigger

  1. #1
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Problem with trigger

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Problem with trigger

    Is this SQL Server? It doesn't look quite right.
    CREATE TRIGGER
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Problem with trigger

    It was supposed to be sql server thing.
    Any ideas why is it wrong?

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Problem with trigger

    Are you getting any error messages? Also, what is the structure of the tables?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Problem with trigger

    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.

    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
    And then some inserts, views and the trigger I said. I'm afraid the whole design is wrong, what become this trigger very important.
    Last edited by asafe; 12-06-2009 at 06:42 AM.

  7. #6
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Problem with trigger

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. trigger rollback, transaction bad syntax problem
    By Bat0u89 in forum Database & Database Programming
    Replies: 7
    Last Post: 06-25-2011, 11:54 AM
  2. What's the best way to trigger a video from flash
    By mehmetkoc in forum Website Design
    Replies: 1
    Last Post: 08-18-2010, 07:00 PM
  3. KeyPress won't trigger
    By anotheruser in forum Visual Basic Programming
    Replies: 11
    Last Post: 08-16-2010, 11:25 AM
  4. trigger
    By kenex in forum Tutorials
    Replies: 0
    Last Post: 11-21-2009, 08:40 AM
  5. Joypad Sound Trigger
    By JohanR in forum General Programming
    Replies: 1
    Last Post: 11-30-2007, 06:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts