CTE“锚点和递归部分之间的类型不匹配”

时间:2018-04-18 15:53:27

标签: sql-server common-table-expression

我知道这个问题已经得到了很多回答,但我找不到错误。

我收到以下错误:

  

锚和递归查询“GroupHierarchy”列“Parent”中的递归部分之间的类型不匹配。

这是查询:

host

原始列WITH GroupHierarchy (UserGroupName, UserGroupID, IsGroup, Parent, Level) AS ( -- Anchor member definition SELECT ug.UserGroupName, ug.UserGroupID, ug.IsGroup, CAST('' as nvarchar(MAX)) as Parent, 0 AS Level FROM KnowledgeVaultMaster.dbo.UserGroup AS ug WHERE ug.IsGroup = 0 UNION ALL -- Recursive member definition SELECT ug.UserGroupName, ug.UserGroupID, ug.IsGroup, CAST(gh.UserGroupName as nvarchar(MAX)) as Parent, Level + 1 FROM KnowledgeVaultMaster.dbo.UserGroup AS ug JOIN KnowledgeVaultMaster.dbo.UserGroupGroup AS ugg ON ug.UserGroupID = ugg.ParentUserGroupId INNER JOIN GroupHierarchy gh ON ugg.UserGroupId = gh.UserGroupID ) -- Statement that executes the CTE SELECT UserGroupName, UserGroupID, IsGroup, Parent, Level FROM GroupHierarchy UserGroup.UserGroupName

编辑:这些是表格(省略了约束和默认值):

nvarchar(255), not null

1 个答案:

答案 0 :(得分:0)

最后我发现了问题。导入的数据库具有兼容级别SQL Server 2012 (110)

我将其更改为SQL Server 2016 (130),现在cte正常工作。