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 1094637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:59:57+00:00 2026-05-16T23:59:57+00:00

A little background information: I have a table called table_a , which has 12

  • 0

A little background information: I have a table called table_a, which has 12 columns. I want to insert or update rows with values for 6 of these columns, while I don’t wanna lose the data in the other 6 columns. And I wanna do this with a parameterized query in C#.

field1 is Unique.

> SELECT * FROM table_a;

+----+--------+--------+---+---------+---------+
| Id | field1*| field2 |...|field11 | field12 |
+----+--------+--------+---+---------+---------+
|  1 | AA     | BB     |...| KK      | LL      |
|  2 | AA     | BB     |...| KK      | LL      |
|  3 | AA     | BB     |...| KK      | LL      |
|  4 | AA     | BB     |...| KK      | LL      |
+----+--------+--------+---+---------+---------+

The Problem is, my first thought was to use REPLACE INTO, unfortunately this will delete the 6 not touched values:

> REPLACE INTO table_a (field1, ..., field6) VALUES ('AA', ...);
> REPLACE INTO table_a (field1, ..., field6) VALUES ('AB', ...);

+----+--------+--------+---+---------+---------+
| Id | field1*| field2 |...| field11 | field12 |
+----+--------+--------+---+---------+---------+
|  1 | AA     | BB     |...| NULL    | NULL    |
|  2 | AB     | BB     |...| NULL    | NULL    |
|  3 | AC     | BB     |...| KK      | LL      |
|  4 | AD     | BB     |...| KK      | LL      |
+----+--------+--------+---+---------+---------+

My second thought was to use INSERT INTO ... ON DUPLICATE KEY UPDATE, but then I’d have to bind the parameters a second time, the first time in the INSERT part and the second time in the UPDATE part, like this:

INSERT INTO table_a (field1, ..., field6)
VALUES(?, ..., ?)
ON DUPLICATE KEY UPDATE
field1 = ?, ..., field6 = ?;

That would preserve my data, but I have to bind the parameters twice.

The third option would be to create another two queries and use the SELECT and INSERT INTO/UPDATE pattern.

So, my question is, how do I do this the smart 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-16T23:59:57+00:00Added an answer on May 16, 2026 at 11:59 pm

    Your second option sounds like a winner for single row updates.

    Your third option is good if you insert/update many rows at once (as it will not matter much that you have two queries then – providing each does only what it is supposed to do).

    UPDATE:
    Digging through documentation one finds that you can bind once if you wish – you can refer to the originally bound values with VALUES()

    UPDATE2:
    Well, actually you can not get to the bound values with VALUES(column), so instead two suggestions that actually might help:

    • did you check about using named parameters (then you would not need to bound them twice)?
    • did you consider stored procedures?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Little Background: I have csv file which has lots of rows and each row
A little background: I have a perl script which is performing a number of
Here is a little background information. I'm working on replacing a dll that has
A little background: I am loading a WPF UI from a database which is
First a little background: I have already managed to connect to a Microsoft SOAP
little background: currently putting together a website that is selling products, many of which
I was wondering if somebody has some insight on this issue. A little background
I have 3 little questions about eric martin's modal window which I can't seem
For a little background information, I've got an application that's running in a loop,
For a little background information, I am trying to sync two calendars (one of

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.