none
Is a Cast/Convert required here?

    Question

  • Just imagine all the right stuff is in between - the question is about the setting the xml @response = to the varchar @xml_output.

    CREATE PROCEDURE [dbo].[sp_test]
    (....
    @response XML OUTPUT   )
    AS
    ...
    @xml_output VARCHAR(max)
    ...

    SET @response = @xml_output  --<<<<here

    Thanks!
    G

    Friday, February 07, 2014 1:34 AM

Answers

  • Nope, according to the SQL Server documentation varchar can be implicitly cast to xml.

    • Proposed as answer by Kalman TothModerator Friday, February 07, 2014 9:07 AM
    • Marked as answer by gen1 Friday, February 07, 2014 7:47 PM
    Friday, February 07, 2014 1:39 AM

All replies

  • Nope, according to the SQL Server documentation varchar can be implicitly cast to xml.

    • Proposed as answer by Kalman TothModerator Friday, February 07, 2014 9:07 AM
    • Marked as answer by gen1 Friday, February 07, 2014 7:47 PM
    Friday, February 07, 2014 1:39 AM
  • You can convert xml to varchar explicitly, here is an example
    declare @varchar varchar(max)
    declare @xml xml
    set @xml=(select name from sys.tables for xml path(''))
    
    set @varchar=cast(@xml as varchar(max))
    select @varchar

     

    Satheesh
    My Blog


    Friday, February 07, 2014 3:02 AM
  • Just didnt realise you were asking about converting varchar data to xml, explicit conversion works there too

    declare @varchar varchar(max)
    declare @xml xml
    set @varchar=stuff((select name from sys.tables for xml path('')),1,0,'')
    
    set @xml=@varchar ---- implicit conversion is allowed
    select @xml


    Satheesh
    My Blog



    Friday, February 07, 2014 3:04 AM
  • gen1,

    Shouldnt be a problem!! As long as the @xml_output varchar variable has a valid xml format, no explicit casting is needed. Automatically the conversion is done.

    For example,

    declare @xml_output varchar(max)='<root><data>abc</data></root>'
    declare @response xml
    set @response=@xml_output
    select @response


    Thanks,
    Jay
    <If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

    Friday, February 07, 2014 3:07 AM