943,923 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 660
  • MS SQL RSS
Jul 3rd, 2009
0

Error in If statement

Expand Post »
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:
MS SQL Syntax (Toggle Plain Text)
  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!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
S2009 is offline Offline
67 posts
since Jun 2009
Jul 5th, 2009
0

Re: Error in If statement

MS SQL Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Generate Alpha-Numeric Values in SQL SERVER
Next Thread in MS SQL Forum Timeline: How do i sendt Text and HTML with sp_send_dbmail?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC