Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i have 2 tables : Serials, ProdConfirm
i want to add 2 columns from Serials table to ProdConfirm table and both columns in tables are similar (i basically want to copy 2 columns to another table)

i have came up with a sql syntax but i cant add two tables at the same time
can some one help me ?

code :

SQL
INSERT INTO   ProdConfirm (Serial, [Group])
SELECT  Serial,[Group]
FROM Serials
WHERE [Group] = ? 


i am using oledb
Posted
Updated 2-Jul-13 9:57am
v2
Comments
thanh_bkhn 3-Jul-13 2:23am    
What is the ProdConfirm table structure, and what is the rule of copying? You have to copy data from Serials to a record of the same "ID", or the ProdConfirm table is just empty, and you can copy 2 columns of all records into it?

If I understand you correctly, you have two columns in Table_1 and two columns in Table_2, both with the same data types. You want to combine these into a single data set, then insert the result into Table_3.

You can approach this by first creating a SELECT statement to get the single data set from the two tables. You can do this with a UNION:
SQL
SELECT Coll AS Column_A, Col2 AS Column_B
FROM Table_1

UNION

SELECT Col3 AS Column_A, Col4 AS Column_B
FROM Table_2
This will return a data set with two columns, populated by the intersection of data from the two tables. That is to say, each row will be distinct. If you need to get any duplicates, instead use UNION ALL You do not need to alias the column names, but if the same position (say, Col1 and Col3) have different names in the different tables, the resulting column in the data set will not have any name.

The second step is to use this for your insert. The result might look like this:
SQL
INSERT INTO Prod_Confirm (Serial, Group) (
  SELECT Col1 AS Serial, Col2 AS Group FROM Table_1
  UNION
  SELECT Col3 AS Serial, Col2 AS Group FROM Table_2
)
SQL will run the selection, then use the resulting data set for the insert.
 
Share this answer
 
Comments
KababChi 2-Jul-13 16:32pm    
tnx for answering but NO i think u misunderstood
i want to copy 2 columns from Table 1 to Table 2
{again i cant even do UNION in my query because it generates sql error i cant figure a way to add relation between 2 tables}
1.First see the data types and size
( e.g varchar(36) ) of the desired columns in Serials table
by using:
SQL
sp_help Serials

2.Then by using alter table querry add those columns with same name, size, and data types in ProdConfirm table. simple!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900