0

I have a stored procedure that selects a aggregated SUM into a local variable.

When I run the code (A Select into Statement) in MYSQL Workbench without running the Stored Procedure and then Select the Variable. I see the result I expect.

When the exact same select into statement is run within the Workbench, or external application, by calling the stored procedure. The local variable always return NULL.

Does any one know why it works outside the Stored Procedure but NOT when running the procedure.

I have included the stored Procedure code and some examples of WorkBench returning the results and not returning the results in the proc.

I even hard coded the Input Variables. It still doesn't work.

Cheers.

Tim.

DROP PROCEDURE MPSH.proc_entry_overallStatusUpdate;

DELIMITER //
CREATE PROCEDURE MPSH.proc_entry_overallStatusUpdate
(IN IN_entryID int(11), IN_entryStatusID int(11))
BEGIN   

    DECLARE approval int;

    SELECT  
        SUM(approval) 
    INTO 
        @approvalScore 
    FROM MPSH.tbl_EntryApprovals 
    WHERE entryID = 8
    AND entryStatusID= 8
    AND sysActive = 1
    GROUP BY entryID, entryStatusID;

   SELECT 'SUM',  @approvalScore ;


    IF @approvalScore   IS NOT NULL THEN 
        IF @approvalScore  >= 2 THEN
            #Approved
            SELECT 'Approve = 2';
            SET approval = 2;
        ELSEIF (@approvalScore  BETWEEN -1 AND 2) THEN
            #Awaiting Review
            SELECT 'Approve = 0';
            SET approval = 0;
       ELSE 
            #Entry Rejected.
            SELECT 'Approve = 3';
            SET approval = 3;
       END IF;


        #Update the Overall Status of an Entry. 
        UPDATE MPSH.tbl_EntryStatus
            SET  overallStatusID = approval
                    ,sysUpdated = NOW()
        WHERE entryStatusID = N_entryStatusID
        AND sysActive = 1;

    END IF;

END // 
DELIMITER ;

CALL MPSH.proc_entry_overallStatusUpdate(8,8);

Thanks for your help.

Running the Code with Hard Coded Values and the result Returns the Result.

Running the actual Stored Procedure and the Select Statement that is in there for Debugging returns NULL.

2
  • You have not been consistent with variable names DECLARE approval int / INTO @approvalScore or types (declared or @) see dev.mysql.com/doc/refman/5.7/en/select-into.html for select into syntax BTW either a declared variable or an @ variable will work - but note a declared variable is not an @ variable. Commented Mar 1, 2018 at 13:12
  • Sorry @P.Salmon just wondering if you can point out the line I am inconsistent on. I am using (@)approvalScore and I am using "approval" but they are different variables. do I need to use the same variable type throughout the whole SP. I am about to read the Doco also. Thanks But any other reasoning could help. Commented Mar 1, 2018 at 13:22

3 Answers 3

4

To store the data into the variable in MySQL, you can do it as below:

Solution1

SET @approvalScore = 0;

SELECT  
    @approvalScore :=SUM(approval)  
FROM MPSH.tbl_EntryApprovals 
WHERE entryID = 8
AND entryStatusID= 8
AND sysActive = 1
GROUP BY entryID, entryStatusID;

Solution2

SELECT  
    SUM(approval) 
INTO 
   approvalScore 
FROM MPSH.tbl_EntryApprovals 
WHERE entryID = 8
AND entryStatusID= 8
AND sysActive = 1
GROUP BY entryID, entryStatusID;

As a good practice, always declare the variable and set it's default value.

Sign up to request clarification or add additional context in comments.

4 Comments

Thnaks however this doesn't work inside a Stored Procedure. I get a Syntax error when trying to use declare @approvalScore int;
Remove the declare and then try... Check both solution
I Tried both solutions above. Option 1 works when I run it natively in Workbench. Just as a command. When I save it in the Stored Proc and Run the Proc. It still returns NULL. Something must be wrong with the way it is handling the variable in the proc.
In one of my SP, I have handle variable as first solution only and it works fing
2

use- declare @approvalScore int; and initialize it

2 Comments

This doesn't work inside a Stored Procedure. I get a Syntax error when . trying to use declare @approvalScore int;
what error did you get? did you use it after your 'DECLARE approval int;' statement?
1

@P.Salmon had the correct point here. After recutting some of my code and re-reading the documentation. I realised I should not mix @vairables with declare variables. I switch the SP to run with just @variables and set each of them when they first appear in the SP and the procedure now works fine.

Thanks.

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.