Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:42:57+00:00 2026-06-17T16:42:57+00:00

In a SQL script which is due to end up feeding a SSRS RDL

  • 0

In a SQL script which is due to end up feeding a SSRS RDL document, I’m trying to leverage CTEs to recursively fetch my data.

The actual SQL code is way too big to copypaste here but I made up this short repro:

declare @temp table
(
id uniqueidentifier not null default(newid()),
name varchar(100) not null default('new'),
value varchar(100) not null default('newValue'),
parentid uniqueidentifier null
)

insert into @temp (id, name, value, parentid) values ('12312312-1234-1234-1234-123412341234', 'o1', 'val1', null)
insert into @temp (id, name, value, parentid) values ('12312312-1234-1234-1234-123412341235', 'o2', 'val2', '12312312-1234-1234-1234-123412341234')
insert into @temp (id, name, value, parentid) values ('12312312-1234-1234-1234-123412341236', 'o3', 'val3', '12312312-1234-1234-1234-123412341234')

;with
    cte(id,name, value, pid, pname, pvalue) as (
        select id, name, value, null, null, null from @temp where parentid is null
        union all
        select r.id, r.name, r.value, a.id, a.name, a.value 
        from @temp r inner join cte a
        on a.id = r.parentid
    )
    select * from cte

The CTE errors out like this (tested in SSMS 2012):

Msg 240, Level 16, State 1, Line 13
Types don't match between the anchor and the recursive part in column "pid" of recursive query "cte".
Msg 240, Level 16, State 1, Line 13
Types don't match between the anchor and the recursive part in column "pname" of recursive query "cte".
Msg 240, Level 16, State 1, Line 13
Types don't match between the anchor and the recursive part in column "pvalue" of recursive query "cte".

I’m pretty sure this is due to the NULL values being selected in the anchor part as opposed to the NOT NULL fields fetched in the recursive part… Can this issue be worked around ?

(I’m open to alternative ways of accomplishing this task.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:42:57+00:00Added an answer on June 17, 2026 at 4:42 pm

    Cast the literal null values to match.

    ;with
    cte(id,name, value, pid, pname, pvalue) as (
        select id, name, value, cast(null as uniqueidentifier),
            cast(null as varchar(100)), cast(null as varchar(100))
        from @temp where parentid is null
        union all
        select r.id, r.name, r.value, a.id, a.name, a.value 
        from @temp r inner join cte a
        on a.id = r.parentid
    )
    

    Otherwise SQL will assume the default type int for each null which won’t match the second part of the union.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script which lists a directory and creates an SQL
I am trying to write T-sql script which will find open records for one
I have a SQL script, which spools data to a file. Sample Existing SQL
I am developing a SQL script in SQL Developer which will obfuscate personal data
I am trying to execute a sql script which creates a procedure and execute
I have an SQL script which creates a scheduled event: CREATE EVENT Daily_1200PM SCHEDULE
I have a SQL script which is extremely large (about 700 megabytes). I am
I have VB Script which is connected with the my database (SQL Server 2008),
I have a 4GB sql data script of my database. I want to execute
We have a simple sql script which needs to be executed against a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.