CTE Problem :By Shubham Saxena

  • Dear friends,

    I need to concatenate a char with in CTE for ex:

    with cteexp(q,col) as

    (select cast(1 as int) p,'X' as col union all select cast(q as int)+1, cast(col as nvarchar(50))+CAST('p' as nvarchar(50)) p from cteexp where cast(q as int)<100 )

    select * from cteexp

    but getting error

    Msg 240, Level 16, State 1, Line 1

    Types don't match between the anchor and the recursive part in column "col" of recursive query "cteexp".

    Kindly, suggest where i am wrong and what is the solution.

    Regards,

    Shubham Saxena

  • Something like this

    WITH cteexp(q, col) AS

    (

    SELECTCAST(1 AS INT) p,

    CAST( 'X' AS NVARCHAR(100) )AS col

    UNION ALL

    SELECT CAST(q + 1 AS INT),

    CAST(col + 'p' AS NVARCHAR(100))

    FROM cteexp

    WHERE q < 100

    )

    SELECT *

    FROM cteexp


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks alot!!

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

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