how do you do "if record doesn't exist create otherwise update" in one SQL statement?

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    how do you do "if record doesn't exist create otherwise update" in one
    SQL statement?

    -Mike
    Mike Guest

  2. Similar Questions and Discussions

    1. "The file you selected does not exist" when the filedefinitely exists?
      Hi all, Using Contribute CS3 against Contribute Publishing Server-- I have the site uploaded and set up within Contribute and CPS. But I am...
    2. Database "template1" does not exist in the system catalog.
      When I try to connect to template1 via psql, I get the following error message: psql: FATAL: Database "template1" does not exist in the system...
    3. Database "template1" does not exist in the system catalog
      I apologize if this is a duplicate of a message I just sent. The message didn't appear to go through. I seem to have encountered a major...
    4. "Duplicate Name Exist On Network" after suspend or undock
      I have a laptop, running WinXP Pro SP1, that doesn't seem to leave the network correctly on a Suspend, Standby, or Undock. The symptoms: When...
    5. Specifying "do not update" values in "additive" UPDATE sprocs
      Hello, I want to write a sproc whose purpose is to perform 'additive' UPDATEs to a given table. By 'additive', I mean I would like the existing...
  3. #2

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    I don't think you can do this in straight ANSI SQL. If you are using
    something like T-SQL (Transact SQL) or PLSQL then it's possible. What DBMS
    are you using?

    Cheers
    Ken

    "Mike" <com> wrote in message
    news:google.com... 


    Ken Guest

  4. #3

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    Assuming SQL Server (never a bad idea to tell us which database you're
    using; "SQL" is pretty generic and ambiguous).

    UPDATE table SET ... WHERE ...
    IF @@ROWCOUNT = 0
    INSERT table(col_list) SELECT val_list

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    "Mike" <com> wrote in message
    news:google.com... 


    Aaron Guest

  5. #4

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    "Aaron [SQL Server MVP]" <noraa> wrote in message news:<#t#phx.gbl>... 

    I'm using Access 2000

    Here's my statement:

    UPDATE MiniStatData SET statValue = NULL WHERE statId = 1 IF
    @@ROWCOUNT = 0 INSERT MiniStatData(miniId, statId, statValue) SELECT
    309, 1, NULL

    I get a "Syntax error (missing operator) in query expression ..."
    error when I try running it. Is it because I am using Access or did I
    write something wrong?

    Thanks,
    Mike
    Mike Guest

  6. #5

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    > UPDATE MiniStatData SET statValue = NULL WHERE statId = 1 IF 

    Yes, it's because you are using Access. My post said "Assuming SQL
    Server"...

    With Access, you might have to do it this way:

    set rs = conn.execute("SELECT COUNT(*) FROM table WHERE ...")
    if rs(0) > 0 then
    ' run update
    else
    ' run insert
    end if

    Maybe Bob could show us a stored query that does the same thing. I'm not
    too big on VBA or how multi-statement stored queries work, because Access is
    far from my cup 'o' tea.

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)


    Aaron Guest

  7. #6

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    Aaron [SQL Server MVP] wrote: 
    >
    > Yes, it's because you are using Access. My post said "Assuming SQL
    > Server"...
    >
    > With Access, you might have to do it this way:
    >
    > set rs = conn.execute("SELECT COUNT(*) FROM table WHERE ...")
    > if rs(0) > 0 then
    > ' run update
    > else
    > ' run insert
    > end if
    >
    > Maybe Bob could show us a stored query that does the same thing. I'm
    > not too big on VBA or how multi-statement stored queries work,
    > because Access is far from my cup 'o' tea.[/ref]

    Nope. No such thing as UPSERT.

    In addition, Jet does not support batched queries. You have to send one
    statement at a time.

    Yet another reason to switch to SQL Server if high throughput is needed.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Guest

  8. #7

    Default Re: how do you do "if record doesn't exist create otherwise update" in one SQL statement?

    > Nope. No such thing as UPSERT.

    <whine>
    Aw, but Oracle has it (MERGE)!
    </whine>

    ;-) for the humor impaired.

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)


    Aaron Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139