how to return an XML query as one field

  • Hi, i have a query using FOR XML AUTO that i need to retrieve in only one big text field (one row, one column), i have been checking some documentation but i cannot achieve this, my goal would be to have the result of the select statement as one varchar field, here's some DDL that will be useful:

    CREATE DATABASE sampleDatabase

    GO

    CREATE TABLE sampleTable ( sampleField1 varchar(50), sampleField2 varchar(50) )

    GO

    INSERT INTO sampleTable

    SELECT 'SampleText1','SampleText2'

    UNION ALL

    SELECT 'SampleTextA','SampleTextB'

    GO

    SELECT * FROM sampleTable FOR XML AUTO

    GO

    DROP DATABASE sampleDatabase

    when i run that code i get a single cell in the results window, i need to save that onto a variable or something else, any help you could provide would be appreciated, thanks.

  • You mean like this? Or am I missing something?

    DECLARE @X XML

    SELECT @X = ( SELECT * FROM sampleTable FOR XML AUTO )

    SELECT @X as [@X]

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • yup, that one did the trick! thanks mate.

  • Happy to help. 🙂

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply