School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,120 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,953 people online right now. Registration is fast and FREE... Join Now!




Error in If statement - Microsoft SQL Server

 

Error in If statement - Microsoft SQL Server

s2009

3 Jul, 2009 - 07:24 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 31


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:

CODE

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!!


User is offlineProfile CardPM
+Quote Post


CamoDeveloper

RE: Error In If Statement - Microsoft SQL Server

3 Jul, 2009 - 09:54 AM
Post #2

D.I.C Head
Group Icon

Joined: 12 Jun, 2009
Posts: 204



Thanked: 12 times
Dream Kudos: 200
My Contributions
Your first IF statement was missing an ")".

CODE

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


~Camo
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:50PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month