0

I have created a stored procedure to count the number of works for each technician using the ID of that technicain and the intended year as a inputs to that stored procedure, but I have stucked to . . . How can I get an array of output in that procedure so that I can use it later in my code in php for example. Here is my stored procedure:

        DROP PROCEDURE `work_count`;
CREATE DEFINER=`root`@`localhost` PROCEDURE `work_count`(IN `id` INT, IN `yearInput` INT) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN 
    SET @x := 1; 
    WHILE @x<13 DO 
       SELECT COUNT(work.workType) 
       FROM work 
       WHERE year(workDate)=yearInput 
          AND work.technicianID = id 
          AND month(workDate)= @x; 
       SET @x := @x+1; 
    END WHILE; 
 END

2 Answers 2

2

If you're executing mulitple queries, you need to create a temporary table to hold your intermediate results, and then just make SELECT * FROM thattemporarytable one of the last things done in your procedure.

Something like this...

BEGIN
   DROP TEMPORARY TABLE IF EXISTS `blah` ( num INT );
   CREATE TEMPORARY TABLE `blah` ( num INT );
   SET @x := 1; 
   WHILE @x<13 DO 
      INSERT INTO blah (num)
      SELECT COUNT(work.workType) 
      FROM work 
      WHERE year(workDate)=yearInput 
            AND work.technicianID = id 
            AND month(workDate)= @x
      ; 
      SET @x := @x+1; 
   END WHILE; 
   SELECT * FROM blah;
   DROP TEMPORARY TABLE blah;
END

or, if you can manage it as a single query, a normal select should work:

BEGIN
   SELECT COUNT(work.workType) 
   FROM work 
   WHERE year(workDate)=yearInput 
         AND work.technicianID = id 
         AND month(workDate) BETWEEN 1 AND 12
   ;
END
Sign up to request clarification or add additional context in comments.

2 Comments

The second thing is how to use that result SELECT* FROM tbl_someTable in a php can it be fetched like normal with while ($row = ...fetch_assoc()){}
If you do not drop the temporary table at the end of the procedure, it can be used on the same database connection (that is the scope of temporary tables); but that should not be necessary. Executing the stored procedure (even from php) should already provide that result set; that is the purpose of that select * even being in the proc.
0

I have used a temporary table to store data into it then publishing it using select statement and dropping the temp table at end of the procedure

BEGIN


  drop TABLE if exists `myDB`.`tbl_temp`;
  CREATE TABLE `myDB`.`tbl_temp` ( `countingWork` INT(11) NOT NULL ) ENGINE = InnoDB;   
  SET @x :=1;
  WHILE @x<=12 DO
      insert into `myDB`.`tbl_temp`
      SELECT COUNT(work.workType) 
      FROM work
      WHERE year(workDate)=yearInput 
            AND work.technicianID = id 
            AND month(workDate)= @x;
      SET @x = @x + 1;
  END WHILE;
  SELECT * from `myDB`.`tbl_temp`;
  drop TABLE `myDB`.`tbl_temp`;
END  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.