Solved

updating SQL server through C# vs2010

Posted on 2012-03-27
8
419 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:Spike_66
ID: 37776587
Many thanks- I noticed the brackets just  before you replied and removing them fixed it.
0
 
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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

860 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