943,875 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 597
  • MS SQL RSS
Jun 30th, 2009
0

Help required for UPDATE TRIGGER

Expand Post »
Hi all,

I have created the Trigger for UPDATING to be invoked when Returnedon column is updated using the following code snnipet.

MS SQL Syntax (Toggle Plain Text)
  1.  
  2. CREATE TRIGGER trg_ForUpdateOnBookIssuedDetails
  3. ON BOOKISSUEDDETAILS
  4. FOR UPDATE
  5. AS begin
  6. declare @Rows1 int,@Rows2 int
  7. IF(UPDATE(ReturnedOn))
  8.  
  9. begin
  10. begin tran
  11. UPDATE nur
  12. SET nur.NumberOfBooksIssued = nur.NumberOfBooksIssued -1
  13. FROM NewUserRegister nur INNER JOIN INSERTED i
  14. ON i.IssuedTo=nur.UserName
  15.  
  16. SET @Rows1 = @@RowCount
  17.  
  18. UPDATE lbd SET Inventory = Inventory +1
  19. FROM LIBRARYBOOKDETAILS lbd INNER JOIN
  20. inserted i ON lbd.BookID = i.BookId
  21.  
  22. SET @Rows2 = @@rowcount
  23.  
  24. IF @Rows1 * @rows2 >0
  25. commit tran
  26. else
  27. begin
  28. raiserror('Error Updating the Database',16,-1)
  29. rollback tran
  30. end
  31. end
  32. end
Now I want To perform this Updation only if the RETURNEDON column is null. If the ReturnedOn column consists of some other value then the Updation of records should not take place.

Can anyone help me out in performing this task?

Please help me out.

Thanks in advance!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
S2009 is offline Offline
67 posts
since Jun 2009
Jun 30th, 2009
0

Re: Help required for UPDATE TRIGGER

Try to change:
if(update(ReturnedOn))
To:
IF (SELECT ReturnedOn FROM INSERTED) IS NULL
Last edited by peter_budo; Jul 2nd, 2009 at 4:55 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 18
Solved Threads: 0
Light Poster
NextCom is offline Offline
26 posts
since Dec 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: Error in Trigger Using TRY CATCH AND IF ELSE
Next Thread in MS SQL Forum Timeline: Table Chaos





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


Follow us on Twitter


© 2011 DaniWeb® LLC