Skip to content

[BUG] Error when JOIN for CTE #665

Description

@hiroyuki0415
DECLARE @id AS UNIQUEIDENTIFIER = (
    SELECT
        b.businessunitid
    FROM
        businessunit AS b
    WHERE
        b.parentbusinessunitid IS NULL
);
-- or
-- DECLARE @id AS UNIQUEIDENTIFIER = {No record exists with this GUID};

WITH
    cte AS (
        SELECT
            e.businessunitid,
            unpvt.*
        FROM
            businessunit AS e
            OUTER APPLY (
                VALUES
                    ('name', CAST(e.name AS NVARCHAR(MAX))),
                    (
                        'parentbusinessunitid',
                        CAST(e.parentbusinessunitid AS NVARCHAR(MAX))
                    )
            ) AS unpvt (columnName, [value])
        WHERE
            e.businessunitid IN (@id)
    )
SELECT
    cte1.columnName,
    cte1.value,
    cte2.value
FROM
    cte AS cte1
    LEFT OUTER JOIN cte AS cte2 ON cte1.columnName = cte2.columnName;

When I run this sql I get the following error:

Object reference not set to an instance of an object

Environment

  • SQL 4 CDS edition: XrmToolBox
  • Results of SELECT @@VERSION:
Microsoft Dataverse - 9.2.25034.202
	SQL 4 CDS - 9.6.0.0
	2 23 2025 06:26:13
	Copyright © 2020 - 2024 Mark Carrington

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions