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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:51:30+00:00 2026-06-04T20:51:30+00:00

I tried some, but I couldn’t find the solution, somehow I managed to get

  • 0

I tried some, but I couldn’t find the solution, somehow I managed to get this result.

Here is the query:

DELIMITER ##
CREATE PROCEDURE test1(start_date DATE,end_date DATE)
BEGIN
   DECLARE done INT DEFAULT FALSE;   
   DECLARE a INT;
   DECLARE present INT;
   DECLARE total INT;
   -- Declare the cursor
   DECLARE id CURSOR
   FOR
   SELECT staff_id FROM ost_staff;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
   -- Open the cursor
   DROP TEMPORARY TABLE IF EXISTS reports;
   CREATE TEMPORARY TABLE IF NOT EXISTS reports
   (
    staff_id INT(10),
    present INT(10),
total INT(10)
   );
   OPEN id;
   read_loop: LOOP
   FETCH id INTO a;
   IF done THEN
   LEAVE read_loop;
   END IF;
   INSERT INTO reports(staff_id,present,total)
   SELECT (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1     DAY),start_date) AS total
    FROM effort_frequency E 
RIGHT OUTER JOIN time_intervals I ON I.interval_start = E.log_date 
AND E.staffid=a AND E.log_date BETWEEN start_date AND end_date
LEFT OUTER JOIN ost_holidays H ON H.holiday_date = I.interval_start
WHERE DATE_FORMAT(I.interval_start,'%a') = 'Sun' OR H.holiday_date = I.interval_start OR E.total_effortspent IS NOT NULL;

   -- Close the cursor   
   END LOOP;
   CLOSE id;
END ##

I got the below result:

+----------+-----------------+
| staff_id | present | total |
+----------+---------+-------+
|  (NULL)  |   23    |  24   |
|  (NULL)  |   22    |  24   |
+----------+---------+-------+

I’m getting (NULL) for staff_id, How can I get the staff_id’s there ?
I tried using declared variable ‘a’ in insert statement, but at that time I got only staff_id, I didn’t get the other 2 fields, I can’t get the staff_id from the select inside insert statement coz there is some problem.

Now what i need is I need to insert the staff_id from the variable ‘a’ into that temporary table.

note: I’m really new to this stored procedure, but somehow managed till here, Its good if I get some detail on how to use the Select inside Insert including the solution for this.

  • 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-06-04T20:51:32+00:00Added an answer on June 4, 2026 at 8:51 pm

    Try this –

    SELECT a, (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1     DAY),start_date) AS total
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried to do some searching on this, but couldn't find exactly what I needed.
I'm guessing there is a simple solution here, but I've tried some things and
I've tried to find some info on the Internet, but couldn't. I'm new to
I've searched around, and saw some tips, but still couldn't find the solution to
I tried to find some clue in the list but I couldn't, so sorry
I've tried quite some fixes i found on stackoverflow and elsewhere but I couldn't
I tried following some of the code from here and here , but I
I have tried to find some decent documentation on traversing in jQuery, but have
I have tried to do some reading about it, but I failed to find
Templates may not be the answer here but I tried some inheritance and that

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.