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

  • Home
  • SEARCH
  • 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 188295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:00:42+00:00 2026-05-11T16:00:42+00:00

The INSERT syntax I’ve been using is this INSERT INTO TableName VALUES (…) The

  • 0

The INSERT syntax I’ve been using is this

INSERT INTO TableName VALUES (...)

The UPDATE syntax I’ve been using is

UPDATE TableName SET ColumnName=Value WHERE ...

So in all my code, I have to generate 2 strings, which would result in something like this

insertStr = "(27, 'John Brown', 102)";
updateStr = "ID=27, Name='John Brown', ItemID=102";

and then use them separately

"UPDATE TableName SET " + updateStr + " WHERE ID=27 " +
"IF @@ROWCOUNT=0 "+
"INSERT INTO TableName VALUES (" + insertStr + ")"

It starts bothering me when I am working with tables with like 30 columns.

Can’t we generate just one string to use on both INSERT and UPDATE?

eg. using insertStr above on UPDATE statement or updateStr on INSERT statement, or a whole new way?

  • 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-11T16:00:42+00:00Added an answer on May 11, 2026 at 4:00 pm

    Some DBMS’ have an extension to do this but why don’t you just provide a function to do it for you? We’ve actually done this before.

    I’m not sure what language you’re using but it’s probably got associative arrays where you can wrote something like:

    pk{"ID"}   = "27"
    val{"Name"} = "'John Brown'"
    val{"ItemID"} = "102"
    upsert ("MyTable", pk, val)
    

    and, if it doesn’t have associative arrays, you can emulate them with multiple integer-based arrays of strings.

    In our upsert() function, we just constructed a string (update, then insert if the update failed) and passed it to our DBMS. We kept the primary keys separate from our other fields since that made construction of the update statement a lot easier (primary key columns went in the where clause, other columns were just set).

    The result of the calls above would result in the following SQL (we had a different check for failed update but I’ve put your @@rowcount in for this example):

    update MyTable set
        Name = 'John Brown',
        ItemID = 102
        where ID = 27
    if @@rowcount=0
        insert into MyTable (ID, Name, ItemID) values (
            27,
            'John Brown',
            102
        )
    

    That’s one solution which worked well for us. No doubt there are others.

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

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want it to be dynamic, so that the… May 11, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer have a look at [http://dev.mysql.com/doc/refman/5.0/en/loop-statement.html1 see if that gives you… May 11, 2026 at 8:13 pm
  • Editorial Team
    Editorial Team added an answer you can extract the strings by name using (?<name>\w+) and… May 11, 2026 at 8:13 pm

Related Questions

I wrote the database schema (only one table so far), and the INSERT statements
I'm trying to figure out the best way to insert a record into a
I had a parametrized insert statement that was working well and I needed to
I'm working on the development of a C++ API which uses custom-designed plugins to
I am writing a data conversion in PL/SQL that processes data and loads it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.