SQL Server: issue with UNION ALL and two Selects

Mike

I am pretty new to SQL and might be doing something wrong here so I hope someone can help me with this.

I have a longer stored procedure that ends with the below. The idea here is to combine two queries into one so that I don't need a separate stored procedure but still have the results separated as one selects by a country (selCountry) and the other selects by a date (selDate).

When I try to save it this way I get the error: "Incorrect syntax near the keyword 'UNION'. When I run them in separate procedures then everything works fine.

-- create final output
SELECT      CONVERT(DATETIME, dDay) AS dDay,
            CONVERT(VARCHAR(11), dDay, 106) AS dLong,
            CONVERT(VARCHAR(10), dDay, 126) AS dShort,
            countries,
            regions,
            mode,
            dName,
            dDesc
FROM        @tempFix
WHERE       countries LIKE '%'+@selCountry+'%'
OR          regions LIKE '%'+@selCountry+'%'
ORDER BY    dDay
UNION ALL
SELECT      CONVERT(DATETIME, dDay) AS dDay,
            CONVERT(VARCHAR(11), dDay, 106) AS dLong,
            CONVERT(VARCHAR(10), dDay, 126) AS dShort,
            countries,
            regions,
            mode,
            dName,
            dDesc
FROM        @tempFix
WHERE       DAY(dDay) = DAY(@selDate)
AND         MONTH(dDay) = MONTH(@selDate)
FOR XML PATH('dDays'), ELEMENTS, TYPE, ROOT('root')

Many thanks in advance for any help with this, Mike.

Joseph B

Please remove the ORDER BY and add it to the very end. You cannot place ORDER BY before UNION / UNION ALL.

Here's more information about this syntax.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server: combine From lines in Union All Selects

From Dev

SQL Server UNION multiple selects in a WHILE statement

From Dev

SQL Server 2008 Join, Union, Multiple Selects?

From Dev

SQL Server - UNION ALL

From Dev

UNION ALL two SELECTs with different column types - expected behaviour?

From Dev

Combine two small Selects in one in SQL Server

From Dev

Multiple recursive union all selects in a CTE SQL query

From Dev

Multiple recursive union all selects in a CTE SQL query

From Dev

group by on union all result SQL Server

From Dev

Union all if the table exists in SQL server 2008

From Dev

group by on union all result SQL Server

From Dev

SQL Server select queries union all and limit

From Dev

SQL Server query: Union vs Distinct union all performance

From Dev

ORACLE merge two SELECTs with UNION adding up

From Dev

Sql Server Union All dont get all rows

From Dev

inner join with two selects sql

From Dev

Combine results of two SQL selects

From Dev

SQL Server connecting several Selects

From Dev

INNER JOIN two tables which were created by UNION ALL in SQL

From Dev

Oracle Optimizer issue with UNION ALL

From Dev

SQL Server 2008r2 Union All Group By

From Dev

SQL Server 2008r2 Union All Group By

From Dev

Union two selects removing duplicates base on some columns (not the full row)

From Dev

I want to do a UNION with two SELECTS and put inside a table

From Dev

SQL Union all statement

From Dev

SQL Union All questions

From Dev

How to use union if i need to "order by" all selects

From Dev

UNION clause performance issue with sql

From Dev

SQL: Combine TWO selects in ONE complex Query