Groups | Blog | Home
all groups > sql server programming > september 2006 >

sql server programming : Combining select result sets


Tracy McKibben
9/28/2006 3:01:11 PM
[quoted text, click to view]

If they all return the same columns, use UNION, or UNION ALL.



--
Tracy McKibben
MCDBA
Anith Sen
9/28/2006 4:27:10 PM
[quoted text, click to view]

Doing at the client side is the best bet, for some server side workarounds
see:
http://www.projectdmx.com/tsql/rowconcatenate.aspx

--
Anith

Arjen
9/28/2006 9:37:52 PM
Hi,

In a sp I have multiple selects.
How can I combine the result sets to one?

Thanks!
Arjen

David Portas
9/28/2006 9:53:50 PM
[quoted text, click to view]

Avoid using SELECT * in your production code. I expect you can use UNION to
get the answer you want:

SELECT col1, col2, col3, ...
FROM table1
WHERE name = 'x'
UNION
SELECT col1, col2, col3, ...
FROM table2
WHERE name = 'x'
UNION
SELECT col1, col2, col3, ...
FROM table3
WHERE name = 'x' ;

With good design this shouldn't usually be necessary because it's a mistake
to repeat the same columns in different tables where the values potentially
have a common key.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

Arjen
9/28/2006 10:45:12 PM
Ah, is there then also a way to use COALESCE?

Thanks,
Arjen


"Tracy McKibben" <tracy@realsqlguy.com> schreef in bericht
news:451C2A07.1010200@realsqlguy.com...
[quoted text, click to view]

Arjen
9/28/2006 11:05:11 PM
Yeah, thats correct. The next step will be to get the values like this:
x;x;x;x; in one field.
I also can do this at client side, but want to give it a try.
I know COALESCE, but this seems not to work with union, because this one
needs to select data not a variable.

Are there options?

Thanks,
Arjen


"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> schreef in bericht
news:%23FAc3A04GHA.3452@TK2MSFTNGP05.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button