[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

updating SQL server through C# vs2010

Hi

I have two identical tables.
I use one as a temporary table for importing data to, so it can be manipulated before updating the real table.

I've tried updaing the real table with:

 string SQL = "INSERT INTO ActualReadings " +
                                " (Date,Time,Thing1, thing2, thing3)" +
                        "SELECT  "  +
                        "(Date,Time,Thing1, thing2, thing3)" +
                            " FROM TempReadings";

                //Declare the Command
                SqlCommand cmd = new SqlCommand(SQL, conn);

                //Make the connection
                conn.Open();
               
                //Execute query
                int NumRows = 0;
                NumRows = cmd.ExecuteNonQuery();
               
but get an error, that I believe is related to the fact both tables have an identity column (ID).

What's the best way around this?

Many thanks.
0
Spike_66
Asked:
Spike_66
  • 5
  • 3
1 Solution
 
plummetCommented:
Hi

Your SQL looks OK to me, you're not trying to insert an identity value unless one of your fields Date, Time, Thing1 etc are an identity field.

What is the error you're getting?
0
 
Spike_66Author Commented:
Incorrect syntax near ','.
 which is refering to the first "," after the word select.

i'm sure it's something to do with both tables having identity columns (ID) which I'm not inserting but obviuosly want to generate.
The column is set up correctly, but just doesn't want to work on a bulk inport.
0
 
plummetCommented:
Try sticking [ and ] around Date and Time, as they are reserved words, and remove those brackets:

string SQL = "INSERT INTO ActualReadings " +
                                " ([Date],[Time],Thing1, thing2, thing3)" +
                        "SELECT  "  +
                        "[Date],[Time],Thing1, thing2, thing3 " +
                            " FROM TempReadings";

Open in new window


Hopefully that will fix it
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Spike_66Author Commented:
Many thanks- I noticed the brackets just  before you replied and removing them fixed it.
0
 
plummetCommented:
Yep, simple as that, not an issue with identity. I'm glad to have helped

Cheers
John
0
 
plummetCommented:
Hi Spike

Are you going to allocate points for my answer?

Many thanks
John
0
 
Spike_66Author Commented:
Apologies- I thought I had done it (It must be old age!!)
0
 
plummetCommented:
I quite understand, judging by your name on here I am 3 years older than you. Now where did I put my glasses...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now