Types don't match between the anchor and the recursive part in column "ParentId" of recursive query "tmp"?

Hi All,

I got below error, how to fix it and what it does mean? Thanks!

Msg 240, Level 16, State 1, Line 14
Types don't match between the anchor and the recursive part in column "ParentId" of recursive query "tmp".

Below is query,

DECLARE @TBL TABLE (RowNum INT, DataId int, DataName NVARCHAR(50), RowOrder DECIMAL(18,2) NULL, ParentId INT NULL)
INSERT INTO @TBL VALUES
(1,    105508, 'A',      1.00, NULL),
(2,    105717, 'A1',      NULL, NULL),
(3,    105718,     'A1',  NULL, NULL),
(4,    105509,      'B', 2.00, NULL),
(5,    105510,    'C',   3.00, NULL),
(6,    105514,     'C1',  NULL, NULL),
(7,    105513,     'D',  4.00, NULL),
(8,    105719,      'D1', NULL, NULL),
(9,    105718,      'D2', NULL, NULL),
(10,    105718,     'D3', NULL, NULL)
 
;with tmp(Original_RowNum, Original_DataId, Original_RowOrder, New_RowOrder, ParentId, pId) as
 (
select RowNum, DataId, RowOrder,cast(RowOrder as DECIMAL(18,2)), ParentId, DataName as pId from @TBL where RowNum = 1
union all
select t.RowNum, t.DataId, t.RowOrder,
case when t.RowOrder is null then cast(tmp.New_RowOrder + 0.01 as DECIMAL(18,2)) else cast(t.RowOrder as DECIMAL(18,2)) end, 
 
case when t.RowOrder is null then tmp.pId end as ParentId, 
case when t.RowOrder is Null then tmp.pId else t.DataName end as pId
from @TBL t 
inner join tmp on tmp.Original_RowNum + 1 = t.RowNum 
 )

 select * from tmp

August 24th, 2015 11:00pm

In your first part (anchor) you're using DataName (nvarchar(50)) as pID and ParentId which is int

In the UNION ALL part of the query you have

case when t.RowOrder IS NULL then tmp.pId as ParentId -- so, you're trying to now use nvarchar(50) for ParentId which is wrong as ParentId

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 11:09pm

oh, thanks!
August 24th, 2015 11:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics