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 2005 Forums
 Transact-SQL (2005)
 Resuls from two Tables

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2007-06-27 : 16:40:39
Hi Guys,
I have 2 tables.
Table1 (Elements)
SectionId | Description
1 | Polices
2 | Risks

Table2 (Topics)
TopicId | SectionId | Description | URL
1 | 1 | Oil | Page1.htm
2 | 1 | Water | Page2.htm
3 | 1 | Petrol | Page3.htm
4 | 1 | Fire | Page4.htm
5 | 2 | oil | Page5.htm
6 | 2 | Fire | Page6.htm

I need a select statement to create a single dataview / datasource
with this result.

ID | ParentID | Text | URL
1 | null | Polices | null (From Elements Table)
2 | null | Risks | null (From Elements Table)
3 | 1 | Oil | Page1.htm (From Topics Table)
4 | 1 | Water | Page2.htm (From Topics Table)
5 | 1 | Petrol | Page3.htm (From Topics Table)
6 | 1 | Fire | Page4.htm (From Topics Table)
7 | 2 | oil | Page5.htm (From Topics Table)
8 | 2 | Fire | Page6.htm (From Topics Table)

Any ideas?

Thanks for your help I really appreciate it.

Best Regards,

Steve.

Steve

phdiwakar
Starting Member

15 Posts

Posted - 2007-06-27 : 17:10:17
[code]
select null as ParentID, Description as [Text], null as URL
from Table1
UNION ALL
select SectionID as ParentID, Description as [Text], URL
from Table2

[/code]

If you need the ID also as indicated in the results, you need to use a temporary table with Identity column and insert all these into that.
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2007-06-27 : 17:17:26
Hi phdiwakar,
Thanks for the reply.
I will try your answer tomorrow, it looks good to me.
I do need to have an ID. How would I create the temporary table and insert the results of the selects into it.

Thanks again for your help, I appreciate it.

Best regards,

Steve.


Steve
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-28 : 09:16:41
Create a temp table with needed columns and do

Insert into #temp(columns)
Your union all query

Madhivanan

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

- Advertisement -