Table2 will be just the backup of Table1.
Main Topics
Browse All TopicsHello Experts,
I need to copy the data from Table1 to Table2
INSERT INTO <Table2>
SELECT * FROM <Table1>
What will be the best way to do?? Insert into small batches? Table 1 contains a million records.
Thanks
Roop
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In that case, you can do it all in one statement including creating the table itself by using
SELECT *
INTO BackupTable
FROM SourceTable
Select/Into is a minimally logged operation (only the metadata and space allocation are logged) so you don't have to worry about having enough log space. It is also very fast since it uses the same mechanism internally as a BCP.
Remember to delete the table you created before you run this as it does not work into an existing table.
Regards,
Bill
Well, a PK is not really a problem in either case, however, it will add overhead to the process of getting data into the new table.
An identity column is another issue all together. The system assigns a value to an identity column as the row is inserted into the table. Normally, you have no control over it and, in fact, if you attempt to insert or update an identity column you will get an error.
Depending on the version of Sybase you are running, there are various methods for dealing with identity columns. For instance, you can turn the IDENTITY INSERT option on which will allow you to insert values into an identity column. You can check the doc on ASE and identity columns for the version you are using.
Regards,
Bill
What you are doing will not work at all. In a nut shell, how does you code know which 100 rows to get/insert each time through the loop.
Chances are you are getting the same 100 rows 1200 times for a total of 120,000 (instead of 12,000). Even if you were going to make this work, SELECT @MAX = @MAX -1 should be @MAX - 100
Unless there is something you haven't told us, drop Table2 and issue a SELECT/INTO and be done with it.
Regards,
Bill
Actually we have drop Table2 and a SELECT/INTO in the existing code.
But the table 1 contains CC# and table 2 is backup table which is taken every month. Table 2 just sit back containing CC# , no one is using them. So i was being asked to use "TRUNCATE" instead of dropping and then insert into in small batches not one big transaction. so thats why i am looking for solution.
You have been sent on a wild goose chase. INSERT/SELECT with chunking of the source table will have substantially more impact than one SELECT/INTO. That plus the fact that you will probably have to add an identity column to the source table in order to get the chunking to work at all. Unless they will let you change the table and then find all the places in the application code that break, I would abandon that idea altogether.
Drop does a truncate as well as removing the metadata entries from the system tables. No big deal at all.
Doing the SELECT/INTO even for the whole thing is much lower overhead than INSERT/SELECT, even in small batches. You have all the logging to contend with as well as the inefficiency of trying to "chunk" the source table.
Also, since SELECT/INTO is minimally logged, there really is not much to the transaction. If you are holding a lock on the source table that is getting in the way, we can probably fix that by setting the transaction isolation level to 0. That and the speed is so much better that it will actually lock things up for less time anyway.
Regards,
Bill
Business Accounts
Answer for Membership
by: reb73Posted on 2009-04-21 at 11:16:09ID: 24197330
Is there an identity column or a numeric PK column in this table?
If yes you could use split it into batches of about 50K-100K and insert within in a loop using ranges..