Equivalent to Informix genxml() function

Santos, Luis 21 Reputation points
2020-11-30T03:31:29.987+00:00

I need to convert a number of Informix views to run on SQL Server.
These views use Informix function genxml() which retrieves and concatenates data from two tables where the second table parses XML output using REPLACE to strip XML tags and keep descriptions.

TABLE A (Request)

Request
1
2
3

TABLE B (Description)

Request,line,description
1,1,'this is a'
1,2,' request description'
2,1,'this is another'
2,2,' one'
3,1,'...and a third one'

The output should look line this:

1,'this is a request description'
2,'this is another one'
3,'...and a third one'

Informix code

create view "dbo".mySample_view (fld_A,fld_B,fld_C) as
select x0.fld_A ,x0.fld_B ,
(select TRIM ( BOTH ' ' FROM REPLACE (REPLACE (REPLACE (informix.genxml(row(TRIM ( BOTH ' ' FROM x1.fld_C ) ),'-' ) ,'<- expression=' ,' ' ),'/>' ,'' ),'"' ,'' ))
from description x1 where ((x1.fld_A = x0.fld_A ) AND (x1.fld_B = x0.fld_B ) ) )
from request x0 ;

Wondering if there is a similar function in SQL server that can produce the same result. I tried to use RAW FOR

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,808 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,558 questions
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,176 Reputation points
    2020-11-30T05:42:26.07+00:00

    Hi @Santos, Luis ,

    What is your version of SQL Server?

    Please refer below and check whethe it is helpful to you.

    DDL:

    create table Request  
    (Request int)  
      
    insert into Request VALUES  
    (1),  
    (2),  
    (3)  
      
    create table Description  
    (Request int,  
    Line int,  
    description varchar(100))  
      
    insert into Description VALUES  
    (1,1,'this is a'),  
    (1,2,' request description'),  
    (2,1,'this is another'),  
    (2,2,' one'),  
    (3,1,'...and a third one')  
    

    SQL Server 2017 and later:

    select a.Request,string_agg(b.description,'') within group (order by b.line) description  
    from Request a   
    inner join Description b   
    on a.Request=b.Request  
    group by a.Request  
    

    SQL Server 2016 and before:

    SELECT a.Request, STUFF((  
                SELECT ' ' + b.description  
                from  Description b   
                where b.Request=a.Request  
                order by b.Line  
                FOR XML PATH('')  
                ), 1, 1, '') description  
    FROM  Request a   
    

    Output:

    Request description  
    1 this is a request description  
    2 this is another one  
    3 ...and a third one  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Hot issues November--What can I do if my transaction log is full?
    Hot issues November--How to convert Profiler trace into a SQL Server table

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2020-11-30T05:43:32.173+00:00

    Check an example:

    declare @tableA table ( Request int )  
    insert @tableA values  
    ( 1 ),   
    ( 2 ),   
    ( 3 )   
      
    declare @tableB table ( Request int, line int, description varchar(max) )  
    insert @tableB values  
    ( 1, 1, 'this is a'            ),  
    ( 1, 2, ' request description' ),  
    ( 2, 1, 'this is another'      ),  
    ( 2, 2, ' one'                 ),  
    ( 3, 1, '...and a third one'   )  
      
      
    select a.Request,  
        (  
            select string_agg(description, '') within group ( order by line )  
            from @tableB  
            where Request = a.Request  
            group by Request  
        )  
    from @tableA a  
    

    The view can be based on this approach.

    0 comments No comments

  2. Santos, Luis 21 Reputation points
    2020-12-01T05:16:55.697+00:00

    That's exactly what I was after.
    Google searches and trial and error were pointing in the right direction but your simple and clear answer confirmed the correct approach.

    Thank you so much.

    0 comments No comments