TSQL CTE Error: Incorrect syntax near ')'
I am developing a TSQL stored proc using SSMS 2008 and am receiving the above error while generating a CTE. I want to add logic to this SP to return every day, not just the days with data. How do I do this? Here is my SP so far:
ALTER Proc [dbo].[rpt_rd_CensusWithChart] @program uniqueidentifier = NULL, @office uniqueidentifier = NULL AS DECLARE @a_date datetime SET @a_date = case when MONTH(GETDATE()) >= 7 THEN '7/1/' + CAST(YEAR(GETDATE()) AS VARCHAR(30)) ELSE '7/1/' + CAST(YEAR(GETDATE())-1 AS VARCHAR(30)) END if exists ( select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#ENROLLEES') ) DROP TABLE #ENROLLEES; if exists ( select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#DISCHARGES') ) DROP TABLE #DISCHARGES; declare @sum_enrollment int set @sum_enrollment = (select sum(1) from enrollment_view A join enrollment_info_expanded_view C on A.enrollment_id = C.enroll_el_id where (@office is NULL OR A.group_profile_id = @office) AND (@program is NULL OR A.program_info_id = @program) and (C.pe_end_date IS NULL OR C.pe_end_date > @a_date) AND C.pe_start_date IS NOT NULL and C.pe_start_date < @a_date) select A.program_info_id as [Program code], A.[program_name], A.profile_name as Facility, A.group_profile_id as Facility_code, A.people_id, 1 as enrollment_id, C.pe_start_date, C.pe_end_date, LEFT(datename(month,(C.pe_start_date)),3) as a_month, day(C.pe_start_date) as a_day, @sum_enrollment as sum_enrollment into #ENROLLEES from enrollment_view A join enrollment_info_expanded_view C on A.enrollment_id = C.enroll_el_id where (@office is NULL OR A.group_profile_id = @office) AND (@program is NULL OR A.program_info_id = @program) and (C.pe_end_date IS NULL OR C.pe_end_date > @a_date) AND C.pe_start_date IS NOT NULL and C.pe_start_date >= @a_date ;WITH #ENROLLEES AS ( SELECT '7/1/11' AS dt UNION ALL SELECT DATEADD(d, 1, pe_start_date) as dt FROM #ENROLLEES s WHERE DATEADD(d, 1, pe_start_date) <= '12/1/11')
The most obvious issue (and probably the one that causes the error message too) is the absence of the actual statement to which the last CTE is supposed to pertain. I presume it should be a SELECT statement, one that would combine the result set of the CTE with the data from the #ENROLLEES table.
And that's where another issue emerges.
You see, apart from the fact that a name that starts with a single # is hardly advisable for anything that is not a local temporary table (a CTE is not a table indeed), you've also chosen for your CTE a particular name that already belongs to an existing table (more precisely, to the already mentioned #ENROLLEES temporary table), and the one you are going to pull data from too. You should definitely not use an existing table's name for a CTE, or you will not be able to join it with the CTE due to the name conflict.
It also appears that, based on its code, the last CTE represents an unfinished implementation of the logic you say you want to add to the SP. I can suggest some idea, but before I go on I'd like you to realise that there are actually two different requests in your post. One is about finding the cause of the error message, the other is about code for a new logic. Generally you are probably better off separating such requests into distinct questions, and so you might be in this case as well.
Anyway, here's my suggestion:
build a complete list of dates you want to be accounted for in the result set (that's what the CTE will be used for);
left-join that list with the #ENROLLEES table to pick data for the existing dates and some defaults or NULLs for the non-existing ones.
It might be implemented like this:
… /* all your code up until the last WITH */ ; WITH cte AS ( SELECT CAST('7/1/11' AS date) AS dt UNION ALL SELECT DATEADD(d, 1, dt) as dt FROM cte WHERE dt < '12/1/11' ) SELECT cte.dt, tmp.[Program code], tmp.[program_name], … /* other columns as necessary; you might also consider enveloping some or all of the "tmp" columns in ISNULLs, like in ISNULL(tmp.[Program code], '(none)') AS [Program code] to provide default values for absent data */ FROM cte LEFT JOIN #ENROLLEES tmp ON cte.dt = tmp.pe_start_date ;