How do you copy records from one table to another?

Hi,

I'd like to copy the entire contents of a table to another table, with the same schema. And I know there must be an easy one line "SELECT * FROM TABLE1" type of command.

Can anybody finish the line above to direct the results to Table2?


Thanks,
Bob
ba272Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachiekCommented:

ds.Tables[0].Copy() will copy both schema and data to new table.


private void CopyDataSet(DataSet myDataSet){
   // Create an object variable for the copy.
   DataSet copyDataSet;
   copyDataSet = myDataSet.Copy();
   // Insert code to work with the copy.
}

Same case for Table

private void CopyDataTable(DataTable myDataTable){
    // Create an object variable for the copy.
    DataTable copyDataTable;
    copyDataTable = myDataTable.Copy();
    // Insert code to work with the copy.
 }


hope you got it.

Sachi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ba272Author Commented:
Hi,

I don't use anyting like what you showed me.  I use SQL commands to handle everything so that it will be portable to other platforms.

I was looking for a "SELECT *" type of command to do the copying.  Do you know it?

Thanks,
Bob
0
sachiekCommented:
Well, That is standard SQL statments.

Above code is by using dataset. That's the difference.

But what is your exact requirement? Do you want it to be done in .net and then insert into SQL Svr or into SQL server using C# directly?


Sachi

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ba272Author Commented:
I have written my entire program using OleDbCommand objects using SQL statements, and this isn't the time for me to learn how to use some new technology.  (I'm just about to reach a deliverable date.)

When I do have the chance, will I get a performance improvement by going that route?

In the meantime, do you know the SQL command?  I think it's:

sql = "SELECT * INTO " + targetTable + " FROM " + sourceTable;


Thanks,
Bob
0
ba272Author Commented:
Sachi,

Thanks for the code.  One day I hope to jump into it.

Bob
0
sachiekCommented:
Ya I knew SQL Command.

So you are building a SQL string then executing that statment.

So sorted out this problem?


Sachi
0
ba272Author Commented:
Yes, I think I got it.  But would I someday benefit by using your approach?  Does it perform better than executing an SQL string?

Thanks,
Bob
0
sachiekCommented:
Well, SQLcommand is much faster. Infact if we use dataset - it will fetch all those records to client side adding more data in viewstate etc etc.

I guess it is good to use SQL Commend which is simple and faster for your requirement.

Sachi
0
ba272Author Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.