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)
 Exporting the results of multiple select statement

Author  Topic 

jonhath
Starting Member

13 Posts

Posted - 2007-08-14 : 14:45:16
Hi,

I'm running several (40) queries on a single table in order to select demographics. My code works but the only problem is I get 40 different result windows. Here's what it looks like:


SELECT TOP (1) submissionkey
FROM dbo.Candidates
WHERE (Sex = 'M') AND (Age >= 18) AND (Age <= 24)
ORDER BY NEWID()

SELECT TOP (2) submissionkey
FROM dbo.Candidates
WHERE (Sex = 'M') AND (Age >= 25) AND (Age <= 44)
ORDER BY NEWID()

...etc


With these two I get two windows:



submissionkey
1 153
2 147


submissionkey
1 145


I'd like to somehow consolidate all the results into one column and export it as a CSV. Each selection needs to be randomized within that selection. I tried some methods that worked if it didn't randomize it (UNION) but I couldn't find a way around it.

ashley.sql
Constraint Violating Yak Guru

299 Posts

Posted - 2007-08-14 : 14:52:42
you can select results to file or you can do bulkinsert

Ashley Rhodes
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-08-14 : 14:58:16
Try inserting into a temp table, then doing one select from that..?
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2007-08-14 : 15:22:20
You could also use UNIONs to join all selected result if they are all have the same number of column and same data type.
Go to Top of Page

jonhath
Starting Member

13 Posts

Posted - 2007-08-14 : 15:35:47
UNION doesn't work with ORDER BY NEWID(). I tried inserting it into a temporary table and it works well.
Go to Top of Page
   

- Advertisement -