Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:47+00:00 2026-05-26T16:41:47+00:00

I am trying to Update a row if it exists, otherwise insert (on duplicate

  • 0

I am trying to Update a row if it exists, otherwise insert (on duplicate key update) in MS Access, with the answer from this question SQL access query- Update row if exists, insert if does not

My insert query has the form:

INSERT INTO mytbl(id, title, notes) 
(SELECT 10, 'test', 'a'
From mytbl
WHERE 10 Not IN (select id from mytbl)
)

But I get an SQL syntax error. What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:41:47+00:00Added an answer on May 26, 2026 at 4:41 pm

    You don’t need the parentheses around the outside SELECT:

    INSERT INTO mytbl(id, title, notes) 
    SELECT TOP 1 10, 'test', 'a'
    FROM DummyTableWithExactlyOneRecord
    WHERE 10 Not IN (select id from mytbl)
    

    To handle the duplicate key error, I added the TOP 1 clause.

    The way your query was originally written, if 10 is not an ID in mytbl, then the SELECT portion of your query would return a record for every existing row in mytbl. Because you are not referencing any fields in the table, they would all be identical rows. For example, if there were four rows in mytbl, you would have:

    10    test    a
    10    test    a
    10    test    a
    10    test    a
    

    It would then try to do an insert for each of those returned rows. The first would succeed and the rest would fail with a duplicate key error. The TOP 1 clause says just use the TOP 1 record. Order does not matter in this case. Since all your fields are literals, every row will be the same.

    Note that you could accomplish the same thing by substituting DISTINCT for TOP 1. I would expect the performance to be worse, though, unless the db engine is smart enough to realize up front that every row will be identical.

    EDIT: DummyTableWithExactlyOneRecord is a local table with exactly one record. As @onedaywhen pointed out in his comment below, if there are no records at all in the table you are SELECTing from, then no record will be inserted.

    Note that if you can guarantee that your dummy table will always have exactly 1 record, then the TOP 1 clause is not necessary. However, I’ve left it in because I think it makes the intent more obvious to a reader who may not know the contents of the dummy table (I’m assuming you will be naming your dummy table something shorter than DummyTableWithExactlyOneRecord).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a row update from php to an mysql database. It
I'm trying to do a single row insert/update on a table but all the
Trying to simple update a row in an SQLiteDatabase Database. I can insert records
I'm trying to search a DataTable for a row I know exists. // This
I am trying to update a field in a table with data from another
So I'm trying to update an object in my MS SQL 2005 database using
I am trying to UPDATE a record if there is a row in the
I am getting this error when trying to delete a row out of a
Im trying to write an SQL query that will check in the table ‘persons’
http://en.wikipedia.org/wiki/Upsert Insert Update stored proc on SQL Server Is there some clever way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.