Insert rows into result of a query

  • Hi,

    I have a table with columns that stores some strings:

    ID col1 col2

    1 aa bb

    2 cd nn

    3 tt hh

    4 uu yy

    I need a SINGLE query(without temp tables or loops) that returns each row from the table followed by a row (Xn, Yn) where n is the row id:

    col1 col2

    aa bb

    X1 Y1

    cd nn

    X2 Y2

    tt hh

    X3 Y3

    uu yy

    X4 Y4

    Thanks,

  • How about this?

    with TableName_cte

    as(

    select ID, Col1, Col2

    from TableName

    UNION

    select ID, 'X'+cast(ID as varchar), 'Y'+cast(ID as varchar)

    from TableName

    )

    select Col1, Col2

    from TableName_cte

    order by ID

    -Supriya

  • It is working.

    Thank you

    ioani

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

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