| | |
Error in If statement
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 67
Reputation:
Solved Threads: 0
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:
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!!
I have used the following code-snippet but it generates the error:
CODING:
MS SQL Syntax (Toggle Plain Text)
CREATE TRIGGER trg_ForUpdateOnBookIssuedDetails ON BOOKISSUEDDETAILS FOR UPDATE AS begin declare @Rows1 int,@Rows2 int IF(UPDATE(ReturnedOn) begin IF EXISTS(SELECT 1 FROM INSERTED) begin UPDATE nur SET nur.NumberOfBooksIssued = nur.NumberOfBooksIssued -1 FROM NewUserRegister nur INNER JOIN INSERTED i ON i.IssuedTo=nur.UserName SET @Rows1 = @@RowCount UPDATE lbd SET Inventory = Inventory +1 FROM LIBRARYBOOKDETAILS lbd INNER JOIN inserted i ON lbd.BookID = i.BookId SET @Rows2 = @@rowcount IF @Rows1 * @rows2 >0 commit tran else begin raiserror('Error Updating the Database',16,-1) rollback tran end end end 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!!
MS SQL Syntax (Toggle Plain Text)
CREATE TRIGGER trg_ForUpdateOnBookIssuedDetails ON BOOKISSUEDDETAILS FOR UPDATE AS begin declare @Rows1 int,@Rows2 int IF(UPDATE(ReturnedOn) begin IF EXISTS(SELECT 1 FROM INSERTED) begin UPDATE nur SET nur.NumberOfBooksIssued = nur.NumberOfBooksIssued -1 FROM NewUserRegister nur INNER JOIN INSERTED i ON i.IssuedTo=nur.UserName SET @Rows1 = @@RowCount UPDATE lbd SET Inventory = Inventory +1 FROM LIBRARYBOOKDETAILS lbd INNER JOIN inserted i ON lbd.BookID = i.BookId SET @Rows2 = @@rowcount IF @Rows1 * @rows2 >0 commit tran else begin raiserror('Error Updating the Database',16,-1) rollback tran end end end 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
![]() |
Similar Threads
- Problem in SQL statement in VB.NET.. Assistance Required. (VB.NET)
- VB Error's I don't know (VB.NET)
- if statement > from1 read from fields.. (C#)
- if statement with dates (PHP)
- Syntax error in INSERT INTO statement (Java)
- Parse error: syntax error, unexpected T_STRING in c:/.......... (PHP)
- find the parse error (PHP)
- runtime error#58 (Visual Basic 4 / 5 / 6)
- Parse Error in IF Statement (PHP)
Other Threads in the MS SQL Forum
- Previous Thread: Generate Alpha-Numeric Values in SQL SERVER
- Next Thread: How do i sendt Text and HTML with sp_send_dbmail?
| Thread Tools | Search this Thread |






