Skip to main content
Participant
December 4, 2012
Answered

Keyword UNION not expected

  • December 4, 2012
  • 1 reply
  • 4084 views

I am trying to get the following union query to process on my coldfusion site:

<cfquery name="committee_record"

         datasource="AS400a CMTLIB"

         maxrows=100

         dbtype="ODBC">

SELECT MBRESMPF.MBMID,

    MBRESMPF.MBCYR,

    MBRESMPF.MBCCD,

    MBRESMPF.MBDCD,

    UCase(COMITEPF.CMNAM) AS COM,

    MBRESMPF.MBMRC,

    UCase(RANKSPF.RKDSC) AS Rank,

    MBRESMPF.MBRPT,

    MBRESMPF.MBESC,

    MBRESMPF.MBERP,

    UCase(DIVISNPF.DVNAM) AS DIV

FROM MBRESMPF INNER JOIN RANKSPF ON MBRESMPF.MBMRC = RANKSPF.RKCDE LEFT JOIN COMITEPF ON MBRESMPF.MBCYR = COMITEPF.CMCYR AND MBRESMPF.MBCCD = COMITEPF.CMCCD AND MBRESMPF.MBDCD = COMITEPF.CMDCD LEFT JOIN DIVISNPF ON MBRESMPF.MBDYR = DIVISNPF.DVDYR AND MBRESMPF.MBDCD = DIVISNPF.DVDCD

WHERE MBRESMPF.MBMRC <> 'ORCDVPRN4' AND MBRESMPF.MBMRC <> 'ORCIPDRNS' AND MBMID = #id_number#

ORDER BY MBRESMPF.MBMID, MBRESMPF.MBCYR DESC

UNION SELECT MBRESMPF.MBMID,

    MBRESMPF.MBCYR,

    MBRESMPF.MBDCD,

    MBRESMPF.MBDCD,

    UCase(DIVISNPF.DVNAM) AS COM,

    MBRESMPF.MBMRC,

    UCase(RANKSPF.RKDSC) AS Rank,

    MBRESMPF.MBRPT,

    MBRESMPF.MBESC,

    MBRESMPF.MBERP,

    UCase(DIVISNPF.DVNAM) AS DIV

FROM MBRESMPF INNER JOIN RANKSPF ON MBRESMPF.MBMRC = RANKSPF.RKCDE LEFT JOIN DIVISNPF ON MBRESMPF.MBDYR = DIVISNPF.DVDYR AND MBRESMPF.MBDCD = DIVISNPF.DVDCD

WHERE MBRESMPF.MBMRC = 'ORCDVPRN4' OR MBRESMPF.MBMRC = 'ORCIPDRNS' AND MBMID = #id_number#

ORDER BY MBRESMPF.MBMID, MBRESMPF.MBCYR DESC

</cfquery>

But I receive the following error message:

Error Diagnostic Information

ODBC Error Code = 37000 (Syntax error or access violation)

[IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0199 - Keyword UNION not expected. Valid tokens: FOR USE SKIP WAIT WITH FETCH OPTIMIZE.

The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (19:1) to (22:23).

This query works for me no problem in Microsoft Access.  Anyone have any suggestions?

Thank you!

This topic has been closed for replies.
Correct answer Adam Cameron.

I'm surprised it works in Access, but in my experience a UNION query usually expects a single ORDER BY at the end (so the one recordset that is the result of the union is ordered, not each recordset that contributes to the union)?  I'm not in a position to test this right now though, sorry.

--

Adam

1 reply

Adam Cameron.Correct answer
Inspiring
December 4, 2012

I'm surprised it works in Access, but in my experience a UNION query usually expects a single ORDER BY at the end (so the one recordset that is the result of the union is ordered, not each recordset that contributes to the union)?  I'm not in a position to test this right now though, sorry.

--

Adam

Participant
December 4, 2012

Hi Adam,

Thanks for your response.  I tried removing the second Order by and am still receiving the same message.  It seems to be bombing out as soon as it reads the UNION SELECT part.  Almost like it can't interpret what UNION means and is expecting another value as it lists OR USE SKIP WAIT WITH FETCH and OPTIMIZE.

Inspiring
December 4, 2012

It's the first ORDER BY that might be the problem, not the second one.  As Adam pointed out, in MS SQL you can only have one ORDER BY and it has to be after any UNIONs.  What database are you using?  I might make some deductions from the name of your datasource, but ...