I am trying to make something like the following work:
WITH results(n, string ) AS (
SELECT 1,'lol'
UNION ALL
SELECT n+1, string + ' lol'
FROM results
WHERE n<6
)
SELECT * FROM results
But SQL doesn't seem to recognize the string concatenation in the second column and returns the error:
Types don't match between the anchor and the recursive part in column "string" of recursive query "results".
My desired output would be something like
1, lol
2, lol lol
3, lol lol lol
and so on
Best Answer
You can cast it to varchar(2000) or varchar(max) depending on your needs so both should have the same data type and size
for value 1, it using an integer/int data type
From BOL related to CTE ,
you can see the data type by using
SQL_VARIANT_PROPERTY
output:
Data type for value/expression
1
can be checked by: