Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2002

    Unanswered: Can I combine these two update statements?

    I have a suspicion that the answer is "no", but I never found out nuffink by not asking, so:

    UPDATE Greetings SET openDate = now() WHERE idGreetings = 123 AND openDate IS NULL
    UPDATE Greetings SET greetingOpenCount = (greetingOpenCount+1) WHERE idGreetings = 123
    My application logic has forced me into a position where I'm running those two straight after each other: I want the "open" count incremented each time a greeting is opened, but I only want to insert the date if it's not already set.

    Is there any way to turn my two DB calls into one?


  2. #2
    Join Date
    Apr 2002
    Toronto, Canada
    Quote Originally Posted by Spudhead
    I have a suspicion that the answer is "no"
    your suspicionating was incorrect
    UPDATE Greetings 
       SET greetingOpenCount = greetingOpenCount + 1 
         , openDate = CASE WHEN openDate IS NULL
                           THEN CURRENT_TIMESTAMP
     WHERE idGreetings = 123 | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts