;WITH CTE AS
(
SELECT [FolderID],[ParentID],CAST([FolderID] AS VARCHAR)+'\' AS [FolderPath]
FROM [Folder] WHERE [FolderID]=28
UNION ALL
SELECT F.[FolderID],F.[ParentID],[FolderPath]+CAST(F.[FolderID] AS VARCHAR)+'\' FROM [Folder] AS F
INNER JOIN CTE ON F.[ParentID]=CTE.[FolderID]
)
SELECT * FROM CTE
为什么这句sql执行不了,提示类型不匹配,但是我都转换为varchar了啊。我的[FolderID]是INT类型。
下面这样则可以执行成功,为什么??
;WITH CTE AS
(
SELECT [FolderID],[ParentID],CAST([FolderID] AS VARCHAR)+'\' AS [FolderPath]
FROM [Folder] WHERE [FolderID]=28
UNION ALL
SELECT F.[FolderID],F.[ParentID],CAST(F.[FolderID] AS VARCHAR)+'\' FROM [Folder] AS F
INNER JOIN CTE ON F.[ParentID]=CTE.[FolderID]
)
SELECT * FROM CTE
如果解决??求解释。。。
SQL sql?server union?all 递归
------解决方案--------------------
--試試以下:
;WITH CTE AS
(
SELECT [FolderID],[ParentID],CAST(CAST([FolderID] AS VARCHAR)+'\' AS VARCHAR) AS [FolderPath]
FROM [Folder] WHERE [FolderID]=28
UNION ALL
SELECT F.[FolderID],F.[ParentID],CAST([FolderPath]+CAST(F.[FolderID] AS VARCHAR)+'\' AS VARCHAR) FROM [Folder] AS F
INNER JOIN CTE ON F.[ParentID]=CTE.[FolderID]
)
SELECT * FROM CTE