• Thanks Matt. Can I request one small help? Below test code has all create table/insert/function and select statement. When I run the select statement, I'm getting escape characters in the output and unable to get complete xml format output. Could you please help me correct the function or select query to produce the output in the right xml format?

    I noticed a procedure in this link which transforms data to JSON. I'd like to try it.

    https://nyquist212.wordpress.com/2014/02/11/tsql-to-json-2/

    CREATE TABLE meas_loc (enty_key bigint,mi_check_pt_rout_key_n bigint,mi_check_pt_pred_key_n bigint,MI_MEAS_LOC_SEQ_N FLOAT);

    CREATE TABLE chkp_cond (enty_key bigint,mi_chkpcond_rout_key_n bigint,mi_chkpcond_pred_key_n bigint,MI_CHKPCOND_SEQ_NUM_N FLOAT) ;

    INSERT INTO meas_loc (enty_key,mi_check_pt_rout_key_n,mi_check_pt_pred_key_n,MI_MEAS_LOC_SEQ_N) VALUES (64251803159,64251705940,64251705940,1);

    INSERT INTO meas_loc (enty_key,mi_check_pt_rout_key_n,mi_check_pt_pred_key_n,MI_MEAS_LOC_SEQ_N) VALUES (64251802979,64251705940,64251705940,2);

    INSERT INTO meas_loc (enty_key,mi_check_pt_rout_key_n,mi_check_pt_pred_key_n,MI_MEAS_LOC_SEQ_N) VALUES (64251802983,64251705940,64251705940,3);

    INSERT INTO chkp_cond (enty_key,mi_chkpcond_rout_key_n,mi_chkpcond_pred_key_n,MI_CHKPCOND_SEQ_NUM_N) VALUES (64252166584,64251705940,64251802983,1);

    INSERT INTO meas_loc (enty_key,mi_check_pt_rout_key_n,mi_check_pt_pred_key_n,MI_MEAS_LOC_SEQ_N) VALUES (64252166585,64251705940,64252166584,1);

    create table lubr_chkp (enty_key bigint, rounte_key bigint, parent_key bigint, enty_seq float, chkp_cond nvarchar(6))

    ;with t as (

    select enty_key,mi_check_pt_rout_key_n rounte_key,mi_check_pt_pred_key_n parent_key,MI_MEAS_LOC_SEQ_N enty_seq, 'true' chkp_cond

    from meas_loc

    union all

    select enty_key,mi_chkpcond_rout_key_n rounte_key,mi_chkpcond_pred_key_n parent_key,MI_CHKPCOND_SEQ_NUM_N enty_seq, 'false' chkp_cond

    from chkp_cond

    )

    insert into lubr_chkp (enty_key , rounte_key , parent_key , enty_seq, chkp_cond )

    select enty_key , rounte_key , parent_key , enty_seq, chkp_cond from t

    go

    drop function SelectChild

    go

    CREATE function SelectChild(@key as bigint)

    returns xml

    begin

    return (

    select

    CONVERT(varchar(100), CAST(enty_seq AS float)) as "@SeqNum",

    enty_key as "@EntityKey",

    chkp_cond as "@IsCheckpoint",

    isnull(CONVERT(varchar(max), cast(dbo.SelectChild(enty_key) as xml)),'null')as "@ListDirectChildren"

    from lubr_chkp

    where parent_key = @key

    order by enty_seq

    for xml path('entity'), type

    )

    end

    go

    WITH PrepareTable (XMLString)AS(SELECT

    CONVERT(varchar(100), CAST(enty_seq AS float)) as SeqNum

    ,enty_key AS EntityKey

    ,chkp_cond as IsCheckpoint

    ,isnull(CONVERT(varchar(max), cast(dbo.SelectChild(enty_key) as xml)),'null')as ListDirectChildren

    FROM lubr_chkp

    WHERE parent_key = 64251705940

    order by enty_seq FOR XML RAW,TYPE,ELEMENTS)SELECT [XMLString]FROM[PrepareTable]