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

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

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" <[email protected]> wrote in message
news:e55h0u$jsh$[email protected]..
> 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
>

Similar Messages

  • SQL : Does autocompletion really work on bound variables

    Inside the SQL-Worksheet i am trying to profit from the
    autocompletion freature of Oracle's SQL-Developper.
    So imagine, i am trying to enter the following SQL code:
    select
    ac.[x]
    from
    account ac
    account ac2
    where
    ac1.name = ac2.name ;
    The "sign" [X] should denote the current position of my
    editor-cursor. After hitting the dot, i would expect a
    popup-selection, allowing me to select one existing
    row-name of my table, but nothing appears.
    Instead of this <my-account>.account. will show me
    the selecton.
    As you may habe noticed in my example, bindung to
    the identifier ac1 and ac2 is important for the
    disambiguation of SQL expressions.
    Thanks for your help
    Groovy

    Code completion for table aliases is not in the current release. It has been logged as an enhancement.

  • Does submit button work on all readers or only adobe reader ?

    does the submit button work in all pdf readers or only in adobe reader ?
    I need to submit FDF.
    Thanks

    Form fields doesn't work in GSview.

  • How does rate plan work if I'm the only iphone with family?

    Both my stepdad and mom will be using normal phones... and I will be using an iPhone 3G. How does the rate plan work? Do my parents have to have their own rate plan, and I have to take my own iPhone line?

    yeah zipidy is wrong.
    im doing the same thing. We have unlimited family texting, which works in conjunction with my iphone, all i had to do was add the 30 dollar data plan.

  • TS1702 Maps program does not work. In standard it only shows a grid. If I ask for a route it shows start and end point and three route boxes but nothing else. It does show directions. If I switch to hybrid it shows to routes but no roads. Background is a

    Maps program does not work. In standard it only shows a grid. If I ask for a route it shows start and end point and three route boxes but nothing else. It does show directions. If I switch to hybrid it shows to routes but no roads. Background is a blur.

    Do you have a question? This is a user to user help forum. Apple is not here only other users like yourself. You don't report problems to Apple here.
    By the way, it might help if you indicated where you are located.
    To complain to Apple use http://www.apple.com/feedback/ipad.html

  • My RDIO app no longer works on my iPad. Does anyone else have this issue?

    My RDIO app is not working on my iPad since they did an update. Does anyone else have this issue?

    Hi klucas54, 
    Thanks for visiting Apple Support Communities. 
    If the RDIO app is not working after the app was updated, start with the helpful troubleshooting tips in this article:
    iOS: An app you installed unexpectedly quits, stops responding, or won’t open
    Best Regards,
    Jeremy

  • I am just wondering does anyone else have a problem in lightroom 4 where some of the tools dont work

    does anyone else have the problem of some tools not working in lightroom 4? for eg. skin smoothing and iris enhance??

    The background music I chose was a standard Apple Loop, but on playback it sounds like someone has cut the music, each time the Loop connects to the extended loops I made when I dragged the end of the initial loop out over a short distance? Did I do it wrong?
    When you extend the loop by dragging the the end of the loop (you'll see a bracket as cursor), you will extend the loop by silence and hear a sharp cut. To make the loop longer, loop it. Grab it in a way, that you'll see a curved arrow as pointer and not the bracket.

  • The 'Swipe to Camera app icon' function is not working.  I swipe across but the camera app doesn't start up at all.  Does anyone else have this problem?  Can it be fixed?

    The 'Swipe to Camera app icon' function is not working.  I swipe across but the camera app doesn't start up at all.  Does anyone else have this problem?  Can it be fixed?

    You don't swipe but actually press, hold and push up.

  • HT4623 I updated my phone last night now it is not working properly. Does anyone else have this problem?  I have a 4S

    I updated my phone last night now it is not working properly. Does anyone else have this problem?  I have a 4S

    Try a hard reset.....
    Press and hold the Wake / Sleep button and the Home button at the same time, keep them both pressed until the Apple Logo appears.

  • HELP!! Does anyone else have their entire keyboard stop working with photoshop?

    This varies for me but most of the time whenever I use Photoshop CC and CS6 my ENTIRE keyboard stops working. Not just the shortcuts but even in other apps too. The only keys that remain functional are the fn, control, option, shift, delete, all of the F keys, and command keys. I am not sure if this is due to me not having enough ram (I have 4 GB). Does anyone else have this problem and if so what did you do to fix it?

    I also have this issue and it's driving me bonkers.
    Latest Photoshop CC, up-to-date.
    Win 8.1 Pro
    Das Keyboard Pro on USB3 port.
    Issue only with Photoshop - no other applications.
    I have tried a brand new machine and a brand new keyboard (replaced my main workstation recently), so I know for sure it's not an issue with any of my hardware. I also know for sure that it's not an issue belonging only to one OS version, as I had the same issue with win7pro and win8.1pro.
    For me the issue is that ALL keyboard keys stop responding completely in Photoshop until I hit the Windows key or otherwise maximise/bring to focus any other application, such as desktop or Outlook etc. Then I immediately bring Photoshop CC back into focus and the keyboard works again. Then after a few moments/clicks/commands the keyboard dies again.
    The issue is 100% local to Photoshop CC, is not related in any way to Wacom drivers (I don't have any installed and don't use a tablet), is not related only to one OS and is not hardware related, as far as I can tell. Once again, I've had these issues on-and-off across 2 different machines with different keyboards.
    I'm wondering if it's related to Teamviewer, since TV creates focus issues with certain other applications/games etc. I will try with TV closed and see if the problem goes away.
    All CC apps are up-to-date.

  • My Iphone 4S cannot play music without headphone. Everything else works, such as alarm, siri, notifications, etc..., except music out loud. I tried everything, but there is no explanation on why it does this.

    My Iphone 4S cannot play music without headphone. Everything else works, such as alarm, siri, notifications, etc..., except music out loud. I tried everything, but there is no explanation on why it does this. If siri and alarm work fine, it cant be a internal sound system problem, and it is just for music without headphones plugged in or any other sound system.

    Hi shanny202.
    Really strange problem you have there. I have a few questions though.
    Please try again and try to make the phone as a new without restoringen with you iCloud-data or backup-data from iTunes. Maybe it is something wrong with the data (strange).
    Anyway, i think you should call Apple Support or visit an Apple Store, don't forgot to make a Genius Bar reservation.
    You locate the nearest Apple Store here: http://www.apple.com/retail/storelist/
    And here is Apples phone numbers around the world for support: http://support.apple.com/kb/HE57

  • TS1398 My new IPad mini has poor wifi reception. Goes in and out.  My IPod, IPhone, Ipad1 and Ipad2 work finer. I think it has wifi antenna issues.  Does anyone else have this problem?

    My new IPad mini has poor wifi reception. It goes in and out.  My IPod, Iphone4, Ipad1 and Ipad2 work fine. What can I do?  Does anyone else have this problem?

    The quickest way (and really the only way) to charge your iPad is with the included 10W USB Power Adapter. iPad will also charge, although more slowly, when attached to a computer with a high-power USB port (many recent Mac computers) or with an iPhone Power Adapter (5W). When attached to a computer via a standard USB port (most PCs or older Mac computers) iPad will charge very slowly (but iPad indicates not charging). Make sure your computer is on while charging iPad via USB. If iPad is connected to a computer that’s turned off or is in sleep or standby mode, the iPad battery will continue to drain.
    Apple recommends that once a month you let the iPad fully discharge & then recharge to 100%.
    How to Calibrate Your Mac, iPhone, or iPad Battery
    http://www.macblend.com/how-to-calibrate-your-mac-iphone-or-ipad-battery/
    At this link http://www.tomshardware.com/reviews/galaxy-tab-android-tablet,3014-11.html , tests show that the iPad 2 battery (25 watt-hours) will charge to 90% in 3 hours 1 minute. It will charge to 100% in 4 hours 2 minutes. The new iPad has a larger capacity battery (42 watt-hours), so using the 10W charger will obviously take longer. If you are using your iPad while charging, it will take even longer. It's best to turn your new iPad OFF and charge over night. Also look at The iPad's charging challenge explained http://www.macworld.com/article/1150356/ipadcharging.html
    Also, if you have a 3rd generation iPad, look at
    Apple: iPad Battery Nothing to Get Charged Up About
    http://allthingsd.com/20120327/apple-ipad-battery-nothing-to-get-charged-up-abou t/
    Apple Explains New iPad's Continued Charging Beyond 100% Battery Level
    http://www.macrumors.com/2012/03/27/apple-explains-new-ipads-continued-charging- beyond-100-battery-level/
    New iPad Takes Much Longer to Charge Than iPad 2
    http://www.iphonehacks.com/2012/03/new-ipad-takes-much-longer-to-charge-than-ipa d-2.html
    Apple Batteries - iPad http://www.apple.com/batteries/ipad.html
    Extend iPad Battery Life (Look at pjl123 comment)
    https://discussions.apple.com/thread/3921324?tstart=30
    New iPad Slow to Recharge, Barely Charges During Use
    http://www.pcworld.com/article/252326/new_ipad_slow_to_recharge_barely_charges_d uring_use.html
    Tips About Charging for New iPad 3
    http://goodscool-electronics.blogspot.com/2012/04/tips-about-charging-for-new-ip ad-3.html
    Prolong battery lifespan for iPad / iPad 2 / iPad 3: charging tips
    http://thehowto.wikidot.com/prolong-battery-lifespan-for-ipad
    iPhone, iPod, Using the iPad Charger
    http://support.apple.com/kb/HT4327
    Install and use Battery Doctor HD
    http://itunes.apple.com/tw/app/battery-doctor-hd/id459702901?mt=8
    In rare instances when using the Camera Connection Kit, you may notice that iPad does not charge after using the Camera Connection Kit. Disconnecting and reconnecting the iPad from the charger will resolve this issue.
     Cheers, Tom

  • When using Facetime, ot works fine at the beginning but it starts to blink for awhile and just turns to black screen. Does anyone else have this problem?

    When i'm using facetime or skype video chat, the camera would work just fine for about 5-10mins and then it starts to blink and then just turns into a black screen. Does anyone else have this problem? HELP!

    Try Resetting the PRAM

  • Why is the GROUP BY clause not working in my Query?

    Dear All,
    Below is the Query for a Summary Debtors Aged Analysis.
    The GROUP BY clause does not seem to be working.
    The Query returns all the unpaid invoices, instead of a single total row for each Customer.
    If a Customer X has 10 unpaid invoices, 10 rows are displayed - one for each invoice.
    I was expecting only 1 row for Customer X, 1 for Customer Y, etc.
    This is what GROUP BY is supposed to do, but it is not doing its work.
    What has gone wrong?
    Thanks
    Leon Lai
    Here's my Query:
    declare @taxdt datetime
    set @taxdt
    /*select 1 from jdt1 t0 where t0.TaxDate*/ = [%1]
    SELECT
    CASE
                 WHEN T0.Account = 1220101 THEN 'Prim Cust'
                 WHEN T0.Account = 1220102 THEN 'Fgn Cust'
                 WHEN T0.Account = 1220103 THEN 'Local Cust'
                 WHEN T0.Account = 1220104 THEN 'Staff Loan' 
                 WHEN T0.Account = 1220105 THEN 'Dep with TP'
                 WHEN T0.Account = 1220106 THEN 'Adv to Cust'
                 WHEN T0.Account = 1220108 THEN 'Sund Drs'
                 ELSE 'Error ! ! !'
    END AS 'Control A/c',
    T1.CardCode AS 'BP Code',
    T2.Notes2 AS 'BP Name',
    SUM ((T0.Debit - T0.Credit)) AS 'Orig. Rs',       
    SUM ((T0.BalDueDeb - T0.BalDueCred)) AS 'Bal. Rs',     
    ((SELECT SUM(T0.BalDueDeb) - Sum(T0.BalDueCred)
        WHERE DateDiff(mm, T0.TaxDate, @taxdt) = 1))    
        AS '1 Mth Ago' 
    /* Similarly for other age brackets*/
    FROM JDT1 T0
    INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
    LEFT OUTER JOIN OCPR T2 ON T1.CardCode = T2.Cardcode
    LEFT OUTER JOIN OJDT T3 ON T0.TransID = T3.TransID
    LEFT OUTER JOIN OINV  T4 ON T3.TransID = T4.TransID
    LEFT OUTER JOIN ORIN  T5 ON T3.TransID = T5.TransID
    WHERE
    T1.CardType = 'C'
    and (Balance) != 0
    and (T0.BalDueDeb - T0.BalDueCred) != 0
    GROUP BY T0.Account, T1.CardCode, T2.Notes2, T0.TaxDate

    Dear Neetu,
    Thanks for your reply.
    This Query is a modification of the Query you posted in SAP B1 SQL TIPS & TRICKS
    http://wiki.sdn.sap.com/wiki/display/B1/SAPB1SQLB-FNDebtorsAgingReportbydate
    So, maybe instead of referring to my Query, let's refer to yours. It may be easier for you to understand me.
    Once I understand the problem, I can adapt your query to suit my requirements
    So, let's start with a clean slate:
    The Query you have posted is for a DETAILED Debtors Aging Report.
    This lists all outstanding invoices, and ages them in the Age Brackets.
    What I want is a SUMMARY Debtors Aging Report.
    This will give the total amount owed by each Customer, and this amount is broken down in the Age Bracket Columns
    There will be a single row listed for each customer, something like this:
    Customer     Total Due     Current      1 Mth          2 Mth         3 Mth  etc
    Alfred       500,000       300,000       200,000
    Charles      800,000                     100,000       300,000       400,000
    How can you modify your query to make it become a Summary Report (1 line for each customer even if he has many invoices)?
    Thanks
    Leon Lai
    Here's your code
    SELECT T1.CardCode, T1.CardName, T1.CreditLine, T0.RefDate, T0.Ref1 'Document Number',
         CASE  WHEN T0.TransType=13 THEN 'Invoice'
              WHEN T0.TransType=14 THEN 'Credit Note'
              WHEN T0.TransType=30 THEN 'Journal'
              WHEN T0.TransType=24 THEN 'Receipt'
              END AS 'Document Type',
         T0.DueDate, (T0.Debit- T0.Credit) 'Balance'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')<=-1),0) 'Future'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>=0 and DateDiff(day, T0.DueDate,'[%1]')<=30),0) 'Current'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>30 and DateDiff(day, T0.DueDate,'[%1]')<=60),0) '31-60 Days'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>60 and DateDiff(day, T0.DueDate,'[%1]')<=90),0) '61-90 Days'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>90 and DateDiff(day, T0.DueDate,'[%1]')<=120),0) '91-120 Days'
         ,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>=121),0) '121+ Days'
    FROM JDT1 T0 INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
    WHERE (T0.MthDate IS NULL OR T0.MthDate > [%1]) AND T0.RefDate <= [%1] AND T1.CardType = 'C'
    ORDER BY T1.CardCode, T0.DueDate, T0.Ref1

  • Why does the Excel source think my query has parameters?

    Part of my current project involves converting an Excel spreadsheet having a number of tabs into an equal number of pipe-delimited flat files using an SSIS package. I've got one data flow task that opens up the excel file and selects the names of all the
    tabs into and object variable. A second data flow task inside a for each loop attempts to read the first 32 columns from each tab. The excel source in the second data flow task is set up to use a sql command from a variable, and the variable is an expression
    that substitutes the tab name into an otherwise-constant sql statement like this:
    SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
    F23, F24, F25, F26, F27, F28, F29, F30, F31, F32 FROM [Securities Portfolio$]
    The "First row has column names" box in the excel connection manager is not marked.
    Here's the thing: this works for most of the tabs. The one above works. However, three of them are causing the following error:
    The SQL command requires 2 parameters, but the parameter mapping only has 0 parameters.
    I've modified the package to print all of the SQL statements used in the log file. The only difference is in the tab name. None have any question marks, which is what I thought SSIS used to mark parameters. I can't figure out what causes this, or even how
    thw failing SQL queries are different than the ones that work:
    SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
    F23, F24, F25, F26, F27, F28, F29, F30, F31, F32 FROM [Capital - Part1$]SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
    F23, F24, F25, F26, F27, F28, F29, F30, F31, F32 FROM [Capital - Part2$]SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
    F23, F24, F25, F26, F27, F28, F29, F30, F31, F32 FROM [Securities Sources$]
    Any help would be appreciated.

    OK, I figured it out, and will write it here in case someone else has a similar issue, because I spent several hours on Google trying to figure out what was going on.
    It turns out that not all of the worksheets actually HAVE 32 columns. It did not occur to me at first that this could be an issue, because when you open an Excel file in Excel, there are always more columns if you just scroll over more. However, the way
    Microsoft Jet reads Excel files, each worksheet has a fixed number of columns.
    The worksheets that were causing the issue were the ones that have fewer than 32 columns. If there are only 30 columns and I try to select F32, Microsoft Jet assumes that F32 is supposed to be the name of a parameter. As far as I can tell, SSIS 2008 does
    not allow you to set up parameter values for use with Jet, but Jet will nevertheless parse your query and determine that there are parameters needed if it can't find some of the column names.
    To get around this, I changed the step that gets the worksheet names to also return the number of columns in each worksheet, and then changed the query to return NULL for any columns that don't exist. So, for a worksheet having only 30 columns, I now run
    this:
    SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
    F23, F24, F25, F26, F27, F28, F29, F30, NULL AS F31, NULL AS F32 FROM [Capital - Part1$]
    And that fixes it.

Maybe you are looking for