Reputation: 123
I am trying to convert
table1
id name
1 aaa
2 bbb
3 ccc
and
table2
id lastname address
1 bbbb asd
2 aaaa asda
3 aaaa asdsd
4 aaaa asdsfd
to
table3
id Fname lName address
1 aaa bbbb asd
2 bbb aaaa asda
3 ccc aaaa asdsd
Is it possible to do using SQL query and stored procedure?
Upvotes: 0
Views: 67
Reputation: 116078
Simply use JOIN
:
SELECT a.name fname,
b.lastname lname,
b.address
FROM table1 a
JOIN table2 b ON a.id = b.id
You can also create table3
as follows:
CREATE TABLE table3 AS
SELECT a.name fname,
b.lastname lname,
b.address
FROM table1 a
JOIN table2 b ON a.id = b.id
This works in almost all known database engines, except for MSSQL (SQLFiddle demo). In MSSQL, use SELECT ... INTO ...
instead:
SELECT a.name fname,
b.lastname lname,
b.address
INTO table3
FROM table1 a
JOIN table2 b ON a.id = b.id
Upvotes: 2
Reputation: 143
Use Cross Join. http://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx
Select * into tableWhatever from A
CROSS JOIN B
You should research a little bit more before posting.
Upvotes: 0
Reputation: 3109
You can do a few things:
what kind of database you are using?
Upvotes: 0