473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to copy rows from one table to another with same structure

12
hi,

I need to copy tableA and tableB from database1 to database2. Both table has the same structure. Only PlanID 1 and PlanID 2's values should be copied
tableA is copied, but tableB is a bit confusing.


DATABASE1
tableA
ItemID | PlanID | col3 | col4 | col5 ......
------ | -------|------|------|----------
21 | 1
22 | 1
23 | 1
24 | 1
25 | 1
26 | 2
27 | 2
28 | 2
29 | 3
30 | 3


tableB
ID | ItemID | col3 | col4 | col5 ......
-- | -------|------|------|-----------
11 | 21
12 | 21
13 | 22
14 | 22
15 | 23
16 | 23



DATABASE2
This is how it should be copied
tableA
ItemID | PlanID | col3 | col4 | col5 ......
------ | -------|------|------|----------
1 | 100
2 | 100
3 | 100
4 | 100
5 | 100
6 | 200
7 | 200
8 | 200


tableB
ID | ItemID | col3 | col4 | col5 ......
-- | -------|------|------|-----------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 3
6 | 3


ItemID -> identity key for tableA
ID -> identity key for tableB
ItemID -> is foreign key in tableB
PlanID -> identity key for Plan table, which i have not given here.

Also, after copying from db1 to db2, other columns(col3,col4....) for tableB in db2 should be same(only the identity field values may be different) as the columns for tableB in db1

Similarly, other columns(col3,col4....) for tableA in db2 should be same(only the identity field values may be different) as the columns for tableA in db1

Is there any way to achieve this using a single Query?
i tried this, but, how to get the ItemID value from tableA

Expand|Select|Wrap|Line Numbers
  1. select @ItemID = ?, col3,col4....
  2. from ..db1.tableB
  3. into ...db2.tableB
  4. left join ???
  5. where PlanID in (1,2)
Oct 4 '11 #1
7 2706
The best advice I can give is download the SSMS tools found here,
www.ssmstoolspack.com

Once installed, you should be able to right click on a specific table and then see options for SSMS tools. Hover over the SSMS tools and there will be an option to Generate Insert Statements. If both of your tables already have the data you need in DB1, and the table structures have already been created in the new database DB2, then this should work perfectly. If the tables are not already created in new database, SQL has its own 'Script table as' tool that will generate the table creation scripts for you. You would do that first and then do the SSMS tools scripting.

Good luck!
Oct 4 '11 #2
ck9663
2,878 Expert 2GB
Yes you can. You have to SELECT INTO TargetTable. The list of columns you will select will not include the identity column from the source table. Instead, create a new column using this function.


Good Luck!!!


~~ CK
Oct 4 '11 #3
dha lak
12
Thank you.
But i want to do the whole transfer in a single query without using stored procedures. This query will be called from csharp console application. Or is it possible to create a temporary stored procedure and delete it once the transfer is complete.
Oct 7 '11 #4
ck9663
2,878 Expert 2GB
There are no stored proc in my suggestion. The link I gave you is a built-in function.

Happy Coding!!!

~~ CK
Oct 7 '11 #5
dha lak
12
I searched for a solution in may forums.
But seems like it can't be done with a single query.

The only way is to use a stored procedure or function.

Thanks for the help
Oct 12 '11 #6
Rabbit
12,516 Expert Mod 8TB
Just multiply by 100 for table a and subtract 20 for table b.
Oct 12 '11 #7
ck9663
2,878 Expert 2GB
Let me see if I got it right.

You have two tables, TableA and TableB.

You want to copy those two tables with all the columns into another server. However the ItemId and ID columns will have a new (restarted) value.

All of that using a single query. Is that right?


~~ CK
Oct 12 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sqlnewbie | last post by:
I'm a newbie to script writing. I'm trying to write a script to copy all data from a table to the same table in a 2nd database. Both databases are on the same server and are identical in design. ...
4
by: Robert Stearns | last post by:
For testing purposes I propose to add a schema (testing, how original) and would like to copy some of my live tables to it, both structure and data. I know I could use something like dump/restore...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
0
by: Ray | last post by:
I tried to make a copy of table but received an error: The Microsoft Jet database engine stopped the process because you and another user are attempting to change the same data at the same time. ...
2
by: Marcin Zmyslowski | last post by:
Hello all! How to copy a table in MS SQL Server 2000 without chaning a structure? I mean, I have one table, which has autoincrement numeric field (ID). When I copy this table by exporting this...
4
by: realjacky | last post by:
How can i copy a table to a new table(structure and data) with primary key remain i have try to use Select * into table from old_table However, i dont have any primary key which old_table have...
6
by: sql_server_user | last post by:
I'm trying to copy all 440 million rows from one table in my SQL Server 2005 db to another table with a different clustering scheme. After a few test inserts that were successful (up to a million...
9
by: fniles | last post by:
I would like to copy a table(s) from SQL Server 2005 to a CVS file and vice versa. I was thinking to use the BCP command line utility, but I have a few questions: 1. The machine where I am...
1
by: akdemirc | last post by:
Hi, My question is about retrieving single records based on a time column, i mean the result set should not include duplicate rows for a unique time value as an example: A B C ...
2
BRawn
by: BRawn | last post by:
Hi guys, I'm struggling to copy rows from one DataGridView to another. This may sound redundant but it's necessary for my Orders project. I have 3 DataGridViews on one form. The first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.