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
 Transact-SQL (2000)
 syntax error near UNION but where??

Author  Topic 

beady
Starting Member

28 Posts

Posted - 2006-08-30 : 04:08:07
Here is the stored procedure which is giving me the error about a syntax error "near UNION". I can't see anything wrong ...



CREATE Procedure Report_getCasesByJudgeFullBench
@idCOURT integer = Null,
@idJUDGE integer = Null,
@idComposition integer,
@citation_date1 datetime = 'January 1, 1753',
@citation_date2 datetime = 'December 31, 9999'
AS
SELECT
DISTINCT
*,
st.description AS legal_status,
o.description AS outcome_description,
cmp.description As court_composition

FROM CASE_ENTITY ce
INNER JOIN COURT co ON co.idCOURT = ce.idCOURT
INNER JOIN FOR_VOTE ON ce.idCASE_ENTITY = FOR_VOTE.idCASE_ENTITY
INNER JOIN JUDGE jdg ON FOR_VOTE.idJudge = jdg.idJUDGE
INNER JOIN COMPOSITION cmp ON ce.idCOMPOSITION = cmp.idCOMPOSITION
INNER JOIN LEGAL_STATUS st ON ce.idLEGAL_STATUS = st.idLEGAL_STATUS
WHERE
(ce.idCOURT = @idCOURT)
AND(jdg.idJUDGE =@idJUDGE )
AND citation_date BETWEEN @citation_date1 AND @citation_date2 Order By citation_date desc

UNION ALL
SELECT
DISTINCT
*,
st.description AS legal_status,
o.description AS outcome_description,
cmp.description As court_composition
FROM CASE_ENTITY ce
INNER JOIN COURT co ON co.idCOURT = ce.idCOURT
INNER JOIN AGAINST_VOTE ON ce.idCASE_ENTITY = AGAINST_VOTE.idCASE_ENTITY
INNER JOIN JUDGE jdg ON AGAINST_VOTE.idJUDGE = jdg.idJUDGE
INNER JOIN COMPOSITION cmp ON ce.idCOMPOSITION = cmp.idCOMPOSITION
INNER JOIN LEGAL_STATUS st ON ce.idLEGAL_STATUS = st.idLEGAL_STATUS
WHERE
(ce.idCOURT = @idCOURT)
AND(jdg.idJUDGE =@idJUDGE )
AND citation_date BETWEEN @citation_date1 AND @citation_date2 Order By citation_date desc

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-30 : 04:14:20
Since you are using "*" and have joined different tables, the number of columns are not equal.

Tables AGAINST_VOTE and FOR_VOTE. And when doing UNION, it is a good practice to identify all columns in the output.

Also UNION takes care of all the DISTINCT for you automatically.



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-30 : 04:18:17
Something like this
CREATE Procedure Report_getCasesByJudgeFullBench
(
@idCOURT integer = Null,
@idJUDGE integer = Null,
@idComposition integer,
@citation_date1 datetime = 'January 1, 1753',
@citation_date2 datetime = 'December 31, 9999'
)
AS

SET NOCOUNT ON

SELECT <all columns explicit specified here>,
st.description AS legal_status,
o.description AS outcome_description,
cmp.description As court_composition,
0 AS ForAgainst
FROM CASE_ENTITY ce
INNER JOIN COURT co ON co.idCOURT = ce.idCOURT
INNER JOIN FOR_VOTE ON ce.idCASE_ENTITY = FOR_VOTE.idCASE_ENTITY
INNER JOIN JUDGE jdg ON FOR_VOTE.idJudge = jdg.idJUDGE AND jdg.idJUDGE = @idJUDGE
INNER JOIN COMPOSITION cmp ON ce.idCOMPOSITION = cmp.idCOMPOSITION
INNER JOIN LEGAL_STATUS st ON ce.idLEGAL_STATUS = st.idLEGAL_STATUS
WHERE ce.idCOURT = @idCOURT
AND ce.citation_date BETWEEN @citation_date1 AND @citation_date2
UNION
SELECT <all columns explicit specified here>,
st.description AS legal_status,
o.description AS outcome_description,
cmp.description As court_composition,
1 AS ForAgainst
FROM CASE_ENTITY ce
INNER JOIN COURT co ON co.idCOURT = ce.idCOURT
INNER JOIN AGAINST_VOTE ON ce.idCASE_ENTITY = AGAINST_VOTE.idCASE_ENTITY
INNER JOIN JUDGE jdg ON AGAINST_VOTE.idJUDGE = jdg.idJUDGE AND jdg.idJUDGE = @idJUDGE
INNER JOIN COMPOSITION cmp ON ce.idCOMPOSITION = cmp.idCOMPOSITION
INNER JOIN LEGAL_STATUS st ON ce.idLEGAL_STATUS = st.idLEGAL_STATUS
WHERE ce.idCOURT = @idCOURT
AND citation_date BETWEEN @citation_date1 AND @citation_date2
ORDER BY ForAgainst,
ce.citation_date desc


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

beady
Starting Member

28 Posts

Posted - 2006-08-30 : 04:49:45
Table joined are different, true, but the do in fact have exactly the same number of columns. So the solution was in using SET NOCOUNT ON and 0 AS ForAgainst etc.

This has resolved the error. Thank YOU!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-30 : 05:13:26
Thanks for the feedback.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -