Search code examples
sql-servert-sqlsql-server-2000coalescefor-xml-path

Sql2000 replicate for xml path('')


I am trying to replicate the following code in SQL 2000 which does not have FOR XML PATH('').

select '"Footer",' +  stuff((SELECT ',' + '"' + server + '"'
FROM servers
FOR XML PATH ('')),1,1,'')

The output is -

 "Footer","MyServer1","MyServer2","MyServer3"

The closest I can get is with the code below but the output is missing the inverted commas around each server name.

declare @result varchar(max)

select @result = COALESCE(@result + ', ', '') + server
                from servers

select '"Footer",' + @result

The output is -

"Footer",MyServer1,MyServer2,MyServer3

Solution

  • Just put the quotes in the expression:

    declare @result varchar(max);
    
    select @result = COALESCE(@result + ', ', '') + '"' + server + '"'
    from servers;
    
    select '"Footer",' + @result;