Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql calling stored function from within a stored procedure causing error

I'm getting a 1064 error when trying to call a stored function from within a stored procedure. It only happens on the line where I try to do this: SET account_id = get_account_id(user);. What is the problem and how can I fix it?

Account ID Stored Functions:

CREATE DEFINER=`aaron`@`%` FUNCTION `get_account_id`(user VARCHAR(255)) RETURNS int(11)
BEGIN
    DECLARE xaccount_id INT DEFAULT 0;

   #Get Account ID and place into variable used when calling stored procedure that builds the tree structure for the leaf node portfolio id
    SELECT account_id
    FROM rst_sessions.session_data
    WHERE  username = user
    ORDER BY update_date DESC LIMIT 1
    INTO xaccount_id;

    RETURN xaccount_id;
END

Stored Procedure that is trying to call the stored Function:

CREATE DEFINER=`aaron`@`%` PROCEDURE `build_report_portfolio_list`(user VARCHAR(255))
    READS SQL DATA
BEGIN

    DECLARE portf_id INT;
    DECLARE portf_name VARCHAR(255);
    DECLARE str_portf_parent_list VARCHAR(455);
    DECLARE done INT DEFAULT 0;
  DECLARE account_id INT;

  SET account_id = get_account_id(user);
END
like image 235
Ronedog Avatar asked Oct 29 '10 19:10

Ronedog


People also ask

How do you call a function inside a stored procedure in MySQL?

CREATE FUNCTION isodd(input_number int) RETURNS int BEGIN DECLARE v_isodd INT; IF MOD(input_number,2)=0 THEN SET v_isodd=FALSE; ELSE SET v_isodd=TRUE; END IF; RETURN(v_isodd); END ; From the MySQL command line, we can invoke our simple stored function in a number of ways.

Can we call a function inside stored procedure?

we can call a function using a select command or by using it in a stored procedure. The function would return a value as a result, therefore we need a parameter in the stored procedure as well so as to hold the result value in it.

Why stored procedure Cannot be called from function in SQL Server?

The function must return a value but in Stored Procedure it is optional. Even a procedure can return zero or n values. Functions can have only input parameters for it whereas Procedures can have input or output parameters. Functions can be called from Procedure whereas Procedures cannot be called from a Function.

Which function Cannot be used in stored procedure in MySQL?

ALTER VIEW . LOAD DATA and LOAD XML . SQL prepared statements ( PREPARE , EXECUTE , DEALLOCATE PREPARE ) can be used in stored procedures, but not stored functions or triggers. Thus, stored functions and triggers cannot use dynamic SQL (where you construct statements as strings and then execute them).


1 Answers

I don't even know if it was possible what I was trying to do, which may have caused the error. But I found a work around by calling the SF as a parameter with the call to the SP and got it to do what I needed it to do.

Code is: CALL build_report_portfolio_list(get_account_id('username_here'));

like image 124
Ronedog Avatar answered Oct 02 '22 16:10

Ronedog