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