Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:12+00:00 2026-05-27T06:58:12+00:00

I am developing a TSQL stored proc using SSMS 2008 and am receiving the

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:58:13+00:00Added an answer on May 27, 2026 at 6:58 am

    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
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a stored proc for an SSRS 2008 report, but I want
Developing a multilingual application in VB.Net 2008, Im able to add resources to forms
I am currently developing an online Auction system using ASP.NET 3.5 and SQLServer 2008.
Developing a VSS writer app: see this error during backup, initiated by windows backup
I am developing an SSRS 2008 report. I created one dataset to give me
Developing a network application, I have a Connection class that manages sending and receiving
Developing a simple game for the iPhone, what gives a better performance? Using a
Developing using MVC-3, Razor, C# Been searching around and cannot find advice I'm looking
Developing mobile apps is a challenging job. Customers want to be present not only
Developing an application using SWT to work in both Linux and Windows. I created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.