Home All Groups Group Topic Archive Search About

Problem with Cursor and Union in select

Author
9 Feb 2006 8:47 PM
Stephane
Hi,

I keep receiving this error when I try to create a cursor:

« ORDER BY items must apprear in the select list if the statement contains a
UNION operator. »

Here's my code

DECLARE cArticles CURSOR FAST_FORWARD
FOR 
(select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
= 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
-@nbDays, getDate())
UNION
select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
= 0 and isEditorial = 0 and categoryId in
(select fk_tbl_category_child_id from tbl_subCategories where
fk_tbl_category_parent_id = @cId))
order by
CASE WHEN @sortId = 0 THEN dateAdded END desc,
CASE WHEN @sortId = 1 THEN dateAdded END,
CASE WHEN @sortId = 2 THEN nbVote END desc,
CASE WHEN @sortId = 3 THEN nbVote END,
CASE WHEN @sortId = 4 THEN nbComments END desc
OPEN cArticles

Does anyone see the problem? Any idea?

Thanks

Stephane

Author
9 Feb 2006 8:59 PM
Mark Williams
Since you are pulling the same columns from the same table, why use UNION at
all; add an OR condition to the WHERE clause to capture rows that would have
matched the query after the UNION.

select id, dateAdded, nbVote, nbComments from vw_articles where

(isFrontPage
= 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
-@nbDays, getDate()))
OR --UNION
(isFrontPage = 0 and isEditorial = 0 and categoryId in
(select fk_tbl_category_child_id from tbl_subCategories where
fk_tbl_category_parent_id = @cId)) )


--

Show quote
"Stephane" wrote:

> Hi,
>
> I keep receiving this error when I try to create a cursor:
>
> « ORDER BY items must apprear in the select list if the statement contains a
> UNION operator. »
>
> Here's my code
>
> DECLARE cArticles CURSOR FAST_FORWARD
> FOR 
> (select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
> = 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
> -@nbDays, getDate())
> UNION
> select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
> = 0 and isEditorial = 0 and categoryId in
> (select fk_tbl_category_child_id from tbl_subCategories where
> fk_tbl_category_parent_id = @cId))
> order by
> CASE WHEN @sortId = 0 THEN dateAdded END desc,
> CASE WHEN @sortId = 1 THEN dateAdded END,
> CASE WHEN @sortId = 2 THEN nbVote END desc,
> CASE WHEN @sortId = 3 THEN nbVote END,
> CASE WHEN @sortId = 4 THEN nbComments END desc
> OPEN cArticles
>
> Does anyone see the problem? Any idea?
>
> Thanks
>
> Stephane
>
Author
9 Feb 2006 9:09 PM
Stephane
I'm about to write a novel: How to complicate simple things... ;-)

Thanks a lot for your help!

Stephane

Show quote
"Mark Williams" wrote:

> Since you are pulling the same columns from the same table, why use UNION at
> all; add an OR condition to the WHERE clause to capture rows that would have
> matched the query after the UNION.
>
> select id, dateAdded, nbVote, nbComments from vw_articles where
>
> (isFrontPage
> = 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
> -@nbDays, getDate()))
> OR --UNION
> (isFrontPage = 0 and isEditorial = 0 and categoryId in
> (select fk_tbl_category_child_id from tbl_subCategories where
> fk_tbl_category_parent_id = @cId)) )
>
>
> --
>
> "Stephane" wrote:
>
> > Hi,
> >
> > I keep receiving this error when I try to create a cursor:
> >
> > « ORDER BY items must apprear in the select list if the statement contains a
> > UNION operator. »
> >
> > Here's my code
> >
> > DECLARE cArticles CURSOR FAST_FORWARD
> > FOR 
> > (select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
> > = 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
> > -@nbDays, getDate())
> > UNION
> > select id, dateAdded, nbVote, nbComments from vw_articles where isFrontPage
> > = 0 and isEditorial = 0 and categoryId in
> > (select fk_tbl_category_child_id from tbl_subCategories where
> > fk_tbl_category_parent_id = @cId))
> > order by
> > CASE WHEN @sortId = 0 THEN dateAdded END desc,
> > CASE WHEN @sortId = 1 THEN dateAdded END,
> > CASE WHEN @sortId = 2 THEN nbVote END desc,
> > CASE WHEN @sortId = 3 THEN nbVote END,
> > CASE WHEN @sortId = 4 THEN nbComments END desc
> > OPEN cArticles
> >
> > Does anyone see the problem? Any idea?
> >
> > Thanks
> >
> > Stephane
> >
Author
9 Feb 2006 9:01 PM
MJKulangara
Use dynamic SQL..ie remove the case handling from after the "order by".

MJKulangara
Author
9 Feb 2006 9:36 PM
David D Webb
Use a derived table for your union, then put the order by in the outer
query...  Or just rewrite that query to use an OR clause instead of the
UNION.

-Dave


Show quote
"Stephane" <Steph***@discussions.microsoft.com> wrote in message
news:2A4849F3-E5FE-4CFA-BDA5-325AFADEC813@microsoft.com...
> Hi,
>
> I keep receiving this error when I try to create a cursor:
>
> « ORDER BY items must apprear in the select list if the statement contains
> a
> UNION operator. »
>
> Here's my code
>
> DECLARE cArticles CURSOR FAST_FORWARD
> FOR
> (select id, dateAdded, nbVote, nbComments from vw_articles where
> isFrontPage
> = 0 and isEditorial = 0 and categoryId = @cId and dateAdded > dateadd(hh,
> -@nbDays, getDate())
> UNION
> select id, dateAdded, nbVote, nbComments from vw_articles where
> isFrontPage
> = 0 and isEditorial = 0 and categoryId in
> (select fk_tbl_category_child_id from tbl_subCategories where
> fk_tbl_category_parent_id = @cId))
> order by
> CASE WHEN @sortId = 0 THEN dateAdded END desc,
> CASE WHEN @sortId = 1 THEN dateAdded END,
> CASE WHEN @sortId = 2 THEN nbVote END desc,
> CASE WHEN @sortId = 3 THEN nbVote END,
> CASE WHEN @sortId = 4 THEN nbComments END desc
> OPEN cArticles
>
> Does anyone see the problem? Any idea?
>
> Thanks
>
> Stephane
>

AddThis Social Bookmark Button