Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

updating SQL server through C# vs2010

Posted on 2012-03-27
8
Medium Priority
?
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

609 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