Error in If statement

Reply

Join Date: Jun 2009
Posts: 67
Reputation: S2009 is an unknown quantity at this point 
Solved Threads: 0
S2009 S2009 is offline Offline
Junior Poster in Training

Error in If statement

 
0
  #1
Jul 3rd, 2009
I am trying to create Update trigger which should be invoked only if the ReturnedOn column is clicked.

I have used the following code-snippet but it generates the error:

CODING:
  1. CREATE TRIGGER trg_ForUpdateOnBookIssuedDetails
  2. ON BOOKISSUEDDETAILS
  3. FOR UPDATE
  4. AS
  5. begin
  6. declare @Rows1 int,@Rows2 int
  7. IF(UPDATE(ReturnedOn)
  8. begin
  9.  
  10. IF EXISTS(SELECT 1 FROM INSERTED)
  11. begin
  12. UPDATE nur
  13. SET nur.NumberOfBooksIssued = nur.NumberOfBooksIssued -1
  14. FROM NewUserRegister nur INNER JOIN INSERTED i
  15. ON i.IssuedTo=nur.UserName
  16.  
  17. SET @Rows1 = @@RowCount
  18.  
  19. UPDATE lbd SET Inventory = Inventory +1
  20. FROM LIBRARYBOOKDETAILS lbd INNER JOIN inserted i
  21. ON lbd.BookID = i.BookId
  22.  
  23.  
  24. SET @Rows2 = @@rowcount
  25.  
  26. IF @Rows1 * @rows2 >0
  27. commit tran
  28. else
  29. begin
  30. raiserror('Error Updating the Database',16,-1)
  31. rollback tran
  32. end
  33. end
  34. end
  35. end

ERROR STATEMENT:

Msg 156, Level 15, State 1, Procedure trg_ForUpdateOnBookIssuedDetails, Line 9
Incorrect syntax near the keyword 'begin'.

I want the trigger to be fired only if the ReturnedOn column is updated, however if anyother column is updated the trigger should not be fired.

Can anyone help me identify the error and changes I should make to rectify this error?

Thanks in advance!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,434
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 439
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: Error in If statement

 
0
  #2
Jul 5th, 2009
  1. CREATE TRIGGER trg_ForUpdateOnBookIssuedDetails
  2. ON BOOKISSUEDDETAILS
  3. FOR UPDATE
  4. AS
  5. begin
  6. declare @Rows1 int,@Rows2 int
  7. IF(UPDATE(ReturnedOn)
  8. begin
  9.  
  10. IF EXISTS(SELECT 1 FROM INSERTED)
  11. begin
  12. UPDATE nur
  13. SET nur.NumberOfBooksIssued = nur.NumberOfBooksIssued -1
  14. FROM NewUserRegister nur INNER JOIN INSERTED i
  15. ON i.IssuedTo=nur.UserName
  16.  
  17. SET @Rows1 = @@RowCount
  18.  
  19. UPDATE lbd SET Inventory = Inventory +1
  20. FROM LIBRARYBOOKDETAILS lbd INNER JOIN inserted i
  21. ON lbd.BookID = i.BookId
  22.  
  23.  
  24. SET @Rows2 = @@rowcount
  25.  
  26. IF @Rows1 * @rows2 >0
  27. commit tran
  28. else
  29. begin
  30. raiserror('Error Updating the Database',16,-1)
  31. rollback tran
  32. end
  33. end
  34. end
  35. end

Line # 27 - begin is missing
Last edited by adatapost; Jul 5th, 2009 at 2:50 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC