Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Union 2 tables

Author  Topic 

John12345
Starting Member

3 Posts

Posted - 2006-11-20 : 04:15:52
I have 2 tables with the same structure as following:

Table A:

ID | Name
-----+-----
1 | AA
2 | BB


Table B:

ID | Name
-----+-----
3 | CC
4 | DD



I want to join them into one as following:

Table A:

ID | Name
-----+-----
1 | AA
2 | BB
3 | CC
4 | DD

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-20 : 04:38:44
Have you tried doing it on your own?

declare @t1 table
(
[id] int,
[name] varchar(20)
)

declare @t2 table
(
[id] int,
[name] varchar(20)
)

insert @t1
select 1, 'AA' union all
select 2, 'BB'

insert @t2
select 3, 'CC' union all
select 4, 'DD'

select * from @t1
union all
select * from @t2



Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

mchohan
Starting Member

39 Posts

Posted - 2006-11-20 : 05:08:56
select ID,Name from TableA
Union All
select ID,Name from TableB

(Union All to include duplicates)
Go to Top of Page

John12345
Starting Member

3 Posts

Posted - 2006-11-20 : 08:37:20
Thanks guys,
I have another question.

I have these 2 tables:

Table A:

ID | Name
---+-----
1 | AA
2 | BB


Table B:

Age | Gender
-----+--------
10 | Male
20 | Female

I want to join them into one as following:

ID | Name | Age | Gender
---+------+-------+--------
1 | AA | 10 | Male
2 | BB | 20 | Female


Is it possible ?

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-20 : 08:43:00
What is the common column between the two tables?

It doesn't make sense unless both tables have something in common.


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

John12345
Starting Member

3 Posts

Posted - 2006-11-20 : 08:57:20
Let's say we have:

Table A:

ID | Name
---+-----
1 | AA
2 | BB


Table B:

ID | Gender
-----+--------
1 | Male
2 | Female


And want:

ID | Name | Gender
---+------+-------
1 | AA | Male
2 | BB | Female
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-20 : 09:00:28
You better start a new topic, if this new question of yours is different from the original question.
This is better from a search point of view and other can benefit from the answers we give you.

select t1.id, t1.name, t2.gender
from tableA t1
inner join tableB t2 on t2.id = t1.id


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-21 : 11:55:45
Also Learn SQL

http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -