Skip to main content
Inspiring
May 26, 2006
Question

SQL: does if/else work as a query or only in a SP?

  • May 26, 2006
  • 2 replies
  • 319 views
I'd like to insert a record if it doesn't exist or UPDATE it if it does. I'm
doing this through a query via my app:

IF EXISTS (SELECT siteID
FROM weSiteMenusXML
WHERE siteID = 1)
INSERT
INTO weSiteMenusXML(siteID)
VALUES (1) ELSE
UPDATE weSiteMenusXML
SET xml = 'boo'
WHERE siteID = 1

Enterprise manager seems to be fine with this syntax, but it doesn't
actually do anything. Is SQL if/else logic only applicable to stored
procedures or is there just something goofy with my syntax?

-Darrel


This topic has been closed for replies.

2 replies

Inspiring
May 26, 2006

> Surely your SQL
> should look something like this:

'doh!

Yea, I had it completely backwards. I feel a tad stupid. ;o)

-darrel

> IF EXISTS (SELECT siteID
> FROM weSiteMenusXML
> WHERE siteID = 1)
> UPDATE weSiteMenusXML
> SET xml = 'boo'
> WHERE siteID = 1
> ELSE
> INSERT
> INTO weSiteMenusXML(siteID)
> VALUES (1)
>
> Pat.
>
>
> "darrel" <notreal@nowhere.com> wrote in message
> news:e55h0u$jsh$1@forums.macromedia.com...
>> I'd like to insert a record if it doesn't exist or UPDATE it if it does.
>> I'm doing this through a query via my app:
>>
>> IF EXISTS (SELECT siteID
>> FROM weSiteMenusXML
>> WHERE siteID = 1)
>> INSERT
>> INTO weSiteMenusXML(siteID)
>> VALUES (1) ELSE
>> UPDATE weSiteMenusXML
>> SET xml = 'boo'
>> WHERE siteID = 1
>>
>> Enterprise manager seems to be fine with this syntax, but it doesn't
>> actually do anything. Is SQL if/else logic only applicable to stored
>> procedures or is there just something goofy with my syntax?
>>
>> -Darrel
>>
>
>


Inspiring
May 26, 2006
Hi Darrel,
If your example below is the same as your actual code in your app then it
won't do anything. You are basically telling SQL to look if a record exists
with a siteID of 1 and if there is such a record then insert a record with
the identical value. If that record doesn't exist then you are instructing
it to do an update on this non-existent row (siteID of 1). Surely your SQL
should look something like this:

IF EXISTS (SELECT siteID
FROM weSiteMenusXML
WHERE siteID = 1)
UPDATE weSiteMenusXML
SET xml = 'boo'
WHERE siteID = 1
ELSE
INSERT
INTO weSiteMenusXML(siteID)
VALUES (1)

Pat.


"darrel" <notreal@nowhere.com> wrote in message
news:e55h0u$jsh$1@forums.macromedia.com...
> I'd like to insert a record if it doesn't exist or UPDATE it if it does.
> I'm doing this through a query via my app:
>
> IF EXISTS (SELECT siteID
> FROM weSiteMenusXML
> WHERE siteID = 1)
> INSERT
> INTO weSiteMenusXML(siteID)
> VALUES (1) ELSE
> UPDATE weSiteMenusXML
> SET xml = 'boo'
> WHERE siteID = 1
>
> Enterprise manager seems to be fine with this syntax, but it doesn't
> actually do anything. Is SQL if/else logic only applicable to stored
> procedures or is there just something goofy with my syntax?
>
> -Darrel
>