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 4232668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:07:26+00:00 2026-05-21T02:07:26+00:00

I’m new to stored procedures and am trying to create one for a school

  • 0

I’m new to stored procedures and am trying to create one for a school project. I wrote an SQL statement that does what I want and just converted it to a stored procedure (probably a bad hack job, but we’re running out of time and have tons of others things to do). We’re using aspnet Membership Services and the entire built-in database.

What I want to do
I’m using 1 table (TimesheetEntry). I want to get all the timesheets of all the employees, sum the hours per weekday to get hours/week and repeat this for the past 4 weeks (week1-week4 below) and for the past 4 months (month1-month4) below. I used nested select statements to do this.

Here is the full error message:

Error 217: Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

Here is the full Stored Procedure:

CREATE PROCEDURE sp_GetTimesheetSummaryReport2
@dateFor DATETIME
AS
SELECT CONVERT(VARCHAR, DATEADD(DAY, -7, GETDATE()), 111) AS startDate,
    CONVERT(VARCHAR, GETDATE(), 111) AS currentDate,
    projId, wpId, empId, 
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Week
        FROM TimesheetEntry
        WHERE empId = t.empId AND tsDate BETWEEN @dateFor AND DATEADD(DAY, -7, @dateFor)
        GROUP BY empId, projId, wpId
    ) AS week1,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Week
        FROM TimesheetEntry
        WHERE empId = t.empId AND tsDate BETWEEN DATEADD(DAY, -7, @dateFor) AND DATEADD(DAY, -14, @dateFor)
        GROUP BY empId, projId, wpId
    ) AS week2,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Week
        FROM TimesheetEntry
        WHERE empId = t.empId AND tsDate BETWEEN DATEADD(DAY, -14, @dateFor) AND DATEADD(DAY, -21, @dateFor)
        GROUP BY empId, projId, wpId
    ) AS week3,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Week
        FROM TimesheetEntry
        WHERE empId = t.empId AND tsDate BETWEEN DATEADD(DAY, -21, @dateFor) AND DATEADD(DAY, -28, @dateFor)
        GROUP BY empId, projId, wpId
    ) AS week4,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Month
        FROM TimesheetEntry
        WHERE empId = t.empId AND MONTH(tsDate) = MONTH(@dateFor)
        GROUP BY empId, projId, wpId
    ) AS month1,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Month
        FROM TimesheetEntry
        WHERE empId = t.empId AND MONTH(tsDate) = MONTH(DATEADD(MONTH, -1, @dateFor))
        GROUP BY empId, projId, wpId
    ) AS month2,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Month
        FROM TimesheetEntry
        WHERE empId = t.empId AND MONTH(tsDate) = MONTH(DATEADD(MONTH, -2, @dateFor))
        GROUP BY empId, projId, wpId
    ) AS month3,
    (
        SELECT (SUM(mon) + Sum(tue) + Sum(wed) + Sum(thu) + SUM(fri) + SUM(sat) + SUM(sun)) AS Month
        FROM TimesheetEntry
        WHERE empId = t.empId AND MONTH(tsDate) = MONTH(DATEADD(MONTH, -3, @dateFor))
        GROUP BY empId, projId, wpId
    ) AS month4
FROM TimesheetEntry t
GROUP BY t.empId, t.projId, t.wpId
ORDER BY t.projId, t.wpId, t.empId;

EXECUTE sp_GetTimesheetSummaryReport '2011/02/01';

My questions

  1. Why am I getting this error? Have I
    exceeded the stack, are there too
    many stored
    procedures/triggers/functions in the
    database, is my nesting too deep?

  2. How can I solve it? Is my stored
    procedure so terrible that I should
    restart using a better syntax?

  • 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-21T02:07:26+00:00Added an answer on May 21, 2026 at 2:07 am

    I’m still not seeing any recursion. To simplify your query, might I recommend making:

    totalhours AS (mon + tue + wed + thu + fri + sat + sun) PERSISTED
    

    into a computed column (perhaps persisted) in your table. Then you aren’t either repeating the logic nor invoking an expensive user-defined function, yet getting some better maintainability.

    It then gives:

        SELECT (SUM(totalhours)) AS Week
        SELECT (SUM(totalhours)) AS Month
    

    I also noticed your query has things like:

    (
        SELECT (SUM(totalhours)) AS Week
        FROM TimesheetEntry
        WHERE empId = t.empId AND tsDate BETWEEN @dateFor AND DATEADD(DAY, -7, @dateFor)
        GROUP BY empId, projId, wpId
    ) AS week1,
    

    Which will return an error if there is more than one row – i.e. if an emp has more than one proj or wp. Because you are treating this as a correlated scalar subquery.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.