Solved

updating SQL server through C# vs2010

Posted on 2012-03-27
8
416 Views
Last Modified: 2012-06-27
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
Comment
Question by:Spike_66
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:plummet
ID: 37772635
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
 

Author Comment

by:Spike_66
ID: 37776413
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
 
LVL 10

Accepted Solution

by:
plummet earned 500 total points
ID: 37776429
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
 

Author Comment

by:Spike_66
ID: 37776587
Many thanks- I noticed the brackets just  before you replied and removing them fixed it.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Expert Comment

by:plummet
ID: 37776605
Yep, simple as that, not an issue with identity. I'm glad to have helped

Cheers
John
0
 
LVL 10

Expert Comment

by:plummet
ID: 37779285
Hi Spike

Are you going to allocate points for my answer?

Many thanks
John
0
 

Author Comment

by:Spike_66
ID: 37781149
Apologies- I thought I had done it (It must be old age!!)
0
 
LVL 10

Expert Comment

by:plummet
ID: 37781255
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now