Showing posts with label recordsets. Show all posts
Showing posts with label recordsets. Show all posts

Friday, March 9, 2012

how to concatenate recordsets

is it possible to concatenate 2 recordsets ?

i have 2 select, and i need then to be displayed one after other.

using union all the results are gathered, ordered like only one select.


thanks for all

You can do this in your SQL query using a union, provided the schemas are identical.

In code, if you have two seoearet recordsets and want to join then, programatically create a datatable and add both these result sets to the new datatable by iterating over them.

Hope this helps
Chris

|||

UNION ALL is the what you need. Here is an introduction for UNION and UNION ALL:http://www.w3schools.com/sql/sql_union.asp

|||

ixxx:

is it possible to concatenate 2 recordsets ?

i have 2 select, and i need then to be displayed one after other.

using union all the results are gathered, ordered like only one select.


thanks for all

What I understod from post is, you asked a question and provided its answer at the same time!

Here is the questoin (you wrote):

ixxx:

is it possible to concatenate 2 recordsets ?

i have 2 select, and i need then to be displayed one after other.

and here is the answer you wrote as well (which is the correct answer :) ):

ixxx:

using union all the results are gathered, ordered like only one select.

I guess there is something missing that you need us to help you with (other than just the UNION).[just a guess becuase you asked the question and answered it in the same post!].

Anyway,,, we are here to help you.

Good luck.

I

|||

"using union all the results are gathered, ordered like only one select."

My guess what the OP means:

using union, all the results are gathered, ordered like only one select.

Not

using union all, the results are gathered, ordered like only one select.

|||

yes that is it

using union, all the results are gathered.

using union all, i get duplicated rows.

thanks for all

how to concatenate 2 recordsets

is it possible to concatenate 2 recordsets ?
i have 2 selects and i want to display all results from the first
select and then the results from the second select query
with paging.
if i use union, all the results will be mixed.
thanks for the help!Post the queries you intend to combine and I'd be willing to take a shot at it for you.

-PatP|||if i use union, all the results will be mixed.not if you use UNION ALL with an ORDER BY clause|||if i use UNION ALL i get duplicated rows

here is one example of a full query i need to be gathered:

0 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or
nome_mod like '%ee%' or estado like '%ee%' or cidade like '%ee%' or
ano like '%ee%' ) and ( nome_marc like '%ff%' or nome_mod like '%ff%'
or estado like '%ff%' or cidade like '%ff%' or ano like '%ff%' ) union
all

1 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or
nome_mod like '%ee%' or estado like '%ee%' or cidade like '%ee%' or
ano like '%ee%' ) union all

2 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) union all

3 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) union all

4 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) union all

5 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) union all

6 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) and ( nome_marc
like '%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade
like '%ee%' or ano like '%ee%' ) and ( nome_marc like '%ff%' or
nome_mod like '%ff%' or estado like '%ff%' or cidade like '%ff%' or
ano like '%ff%' ) union all

7 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) and ( nome_marc
like '%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade
like '%ee%' or ano like '%ee%' ) union all

8 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) union all

9 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) union all

10 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) union all

11 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) and ( nome_marc like '%ee%' or nome_mod like '%ee%' or estado
like '%ee%' or cidade like '%ee%' or ano like '%ee%' ) and ( nome_marc
like '%ff%' or nome_mod like '%ff%' or estado like '%ff%' or cidade
like '%ff%' or ano like '%ff%' ) union all

12 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) and ( nome_marc like '%ee%' or nome_mod like '%ee%' or estado
like '%ee%' or cidade like '%ee%' or ano like '%ee%' ) union all

13 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) union all

14 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) union all

15 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or nome_mod
like '%ee%' or estado like '%ee%' or cidade like '%ee%' or ano like
'%ee%' ) and ( nome_marc like '%ff%' or nome_mod like '%ff%' or estado
like '%ff%' or cidade like '%ff%' or ano like '%ff%' ) union all

16 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or nome_mod
like '%ee%' or estado like '%ee%' or cidade like '%ee%' or ano like
'%ee%' ) union all

17 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) union all

18 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade like
'%ee%' or ano like '%ee%' ) and ( nome_marc like '%ff%' or nome_mod
like '%ff%' or estado like '%ff%' or cidade like '%ff%' or ano like
'%ff%' ) union all

19 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade like
'%ee%' or ano like '%ee%' ) union all
- Hide quoted text -

20 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ff%' or nome_mod like '%ff%' or estado like '%ff%' or cidade like
'%ff%' or ano like '%ff%' )

these numbers are here just to show us how many select statements it has
id like the final result to be ordered w all results from the first query,
then all results from second query and so on

when i use union on it, all results are mixed|||you have 20 select statements that are all doing the same thing!!

i have a feeling they should be just one query, and the WHERE clauses combined with ORs

no wonder UNION ALL returns dupes -- there are lots of dupes, and a given row might satisfy 10 of your queries

however, you forgot to add a row discriminator and an ORDER BY clause

here is an example --select 1 as row_type
, foo
, bar
from table1
union all
select 2
, foo
, bar
from table2
order
by row_type
, foonotice that all the rows from the first select will have 1s in the first column of results, while all the rows from the second select will have 2s in the first column of results, so it is then not possible for a row from the first select to duplicate a row from the second select

this is the true concatenation of result sets

:)|||it gave me duplicated rows.

but thanks for helping

i think i must use one temporary table or a table variable|||In order to avoid many different problems with your original approach, I would suggest using:SELECT *
FROM (SELECT *
, CASE
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 1
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 2
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 3
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
THEN 4
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
THEN 5
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 6
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 7
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 8
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 9
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
THEN 10
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 11
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 11
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 12
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 13
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 14
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 15
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 16
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 17
WHEN 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 18
WHEN 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 19
WHEN 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 20
ELSE 0
END AS DBForums) AS a
WHERE 0 <> DBForums
ORDER BY DBForums
-PatP|||to avoid duplicate rows use just UNION instead of UNION ALL.