views:

157

answers:

2

I have the following query:

    WITH Orders(Id)
AS (
SELECT DISTINCT anfrageid FROM MPHotlineAnfrageAnhang
)
SELECT Id,
(
SELECT CONVERT(VARCHAR(255),anfragetext) + ' | '
FROM MPHotlineAnfrageAnhang
WHERE anfrageid = Id
ORDER BY anfrageid, erstelltam
FOR XML PATH('')
) AS Descriptions
FROM Orders

Its concatenates varchar values of diferents rows grouped by an id. But now i want to include it as a subquery and it gives some errors i cant solve. Simplified example of use:

select descriptions from 
(
    WITH Orders(Id)
    AS (
    SELECT DISTINCT anfrageid FROM MPHotlineAnfrageAnhang
    )
    SELECT Id,
    (
    SELECT CONVERT(VARCHAR(255),anfragetext) + ' | '
    FROM MPHotlineAnfrageAnhang
    WHERE anfrageid = Id
    ORDER BY anfrageid, erstelltam
    FOR XML PATH('')
    ) AS Descriptions
    FROM Orders
) as tx where id=100012

Errors (Aproximate translation from spanish):

-Incorrect sintaxis near 'WITH'.
-Incorrect sintaxis near 'WITH'. If the instruction is a common table expression or a xmlnamespaces clause, the previous instruction must end with semicolon.
-Incorrect sintaxis near ')'.

What im doing wrong?

+3  A: 

Chain your queries as CTEs, like this:

WITH Orders(Id) AS (
    SELECT DISTINCT anfrageid
    FROM MPHotlineAnfrageAnhang
),
OrderDescs AS (
    SELECT Id, (
        SELECT CONVERT(VARCHAR(255),anfragetext) + ' | '
        FROM MPHotlineAnfrageAnhang
        WHERE anfrageid = Id
        ORDER BY anfrageid, erstelltam
        FOR XML PATH('')
        ) AS Description
    FROM Orders
)
SELECT Description
FROM OrderDescs
WHERE Id = 100012 

You can have as many CTEs as you like, each referencing the previous, before the actual query.

Matt Hamilton
Cant see how to apply it :S could you please refactor my example query so i can undertand it in context?
ase69s
@ase69s See how close I got! :)
Matt Hamilton
It works :)! One problem thought, the query I posted was a simplified one to prevent confusion but in the real case the query is very long and the format is like:select t1.field1, t2.field1, field2, field3, field4,(subquery linked by some code),(subquery2 linked by some code),(subquery3 linked by some code)from table t1, table2 t2 where...As you can see te subquerys are autocontained...easier to manage. So how could i reformat your version as to include it like the rest of them?
ase69s
@ase69s, The `subquerys are autocontained...easier to manage`, but those are huge performance hogs, as they run once for each result set row returned. you would be better off if you could rewrite them using joins or derived tables.
KM
Yeah is a known problem but for now...also it would be interesting to know how to do it.
ase69s
A: 

Also, you need to have a semi-colon before a WITH statement.

;with Orders(id)

Or terminate the previous statement with the semi-colon instead.

revelator
If you have seen the test case example there is no previous statement and trying your solution gives: incorrect syntaxis next to ';' and incorrect syntaxis next to ')'
ase69s
Ah, apologies I missed the with in the second code example! Matt Hamiltons method should do what you want without any syntax errors, you can't have a With in a sub-query.
revelator