Understanding CTE Semicolon Placement
问题 When I run this CTE in SQL Server it says the syntax is incorrect by the declare statement. ;WITH cte as ( SELECT tblKBFolders.FolderID from tblKBFolders where FolderID = @FolderID UNION ALL SELECT tblKBFolders.FolderID FROM tblKBFolders INNER JOIN cte ON cte.FolderID = tblKBFolders.ParentFolderID ) declare @tblQueryFolders as table (FolderID uniqueidentifier) insert into @tblQueryFolders SELECT FolderID From cte; But if I move the declare to before the CTE, it runs just fine. declare