本文介绍了公用表表达式(CTE)查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 t为(选择名称,Id,SrID,'avik'为[senior]来自sal,其中SrID = 0 union all 选择sal.name,sal.Id,sal.SrID,sal.name为[senior]来自sal内连接t sal.SrID = t.Id ) select * from t 错误:类型don ' t在锚和递归查询t的列senior中的递归部分之间匹配。 表:Sal(Id,Name,Sal,SrId) 请帮助。 解决方案 由于数据类型/大小的不同,问题在于'avik'。假设sal.name的类型为varchar(100),则会与varchar(4)的'avik'冲突。如果你匹配像cast这样的两种类型('avak'作为VARCHAR(100)),问题就会解决。' 试试这个。 DECLARE @ SAL TABLE ( NAME VARCHAR ( 100 ), ID INT , SRID INT ) INSERT INTO @ SAL (NAME,ID,SRID) SELECT ' avik', 1 , NULL UNION ALL SELECT ' B', 2 , 1 UNION ALL SELECT ' C', 3 , 2 ; t ( 选择名称,Id,SrID,强制转换(' avik' as varchar ( 100 )) as [senior] 来自 @ SAL 其中 SrID IS NULL union all select S.name,S.Id,S.SrID,T.name as [senior] 来自 @ SAL S 内部 join t 上 S.SrID = t.Id ) 选择 * 来自 t with t as(select name,Id,SrID,'avik' as [senior] from sal where SrID=0union allselect sal.name,sal.Id,sal.SrID,sal.name as [senior] from sal inner join t on sal.SrID= t.Id)select * from tError : Types don't match between the anchor and the recursive part in column "senior" of recursive query "t".Table : Sal (Id,Name,Sal,SrId)Please help . 解决方案 The problem is in constant 'avik' because of data type/size difference. Assuming that sal.name is of type varchar(100), will conflict of 'avik' which is of varchar(4). Problem would resolve if you match the both types like cast('avak' as VARCHAR(100)).'Try this.DECLARE @SAL TABLE(NAME VARCHAR(100),ID INT,SRID INT)INSERT INTO @SAL (NAME, ID, SRID)SELECT 'avik', 1, NULLUNION ALLSELECT 'B', 2, 1UNION ALLSELECT 'C', 3, 2;with t as(select name,Id,SrID,cast('avik' as varchar(100)) as [senior] from @SAL where SrID IS NULLunion allselect S.name,S.Id,S.SrID,T.name as [senior] from @SAL S inner join t on S.SrID = t.Id)select * from t 这篇关于公用表表达式(CTE)查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 08:47