S
S
Stepan Davashenevquestion2020-05-09 09:22:47
MySQL
Stepan Davashenevquestion, 2020-05-09 09:22:47

How to fix the function?

help me create a function
to be honest in mysql this is my first time doing this

CREATE DEFINER = CURRENT_USER FUNCTION `GetMailAcc`(`in_domen_id` int)
    RETURNS varchar(255)
BEGIN
    DECLARE result_str VARCHAR(255);
    SELECT COUNT(1) AS result_id  FROM L_mail_account AS MA WHERE MA.domen_id = in_domen_id LIMIT 1;
    IF result_id > 0 THEN
        SELECT id FROM L_mail_account ORDER BY last_upd DESC LIMIT 1;
    ELSE
        SELECT MA.id FROM L_mail_account AS MA WHERE MA.domen_id = in_domen_id LIMIT 1;
    END IF;
    RETURN id;
END;


says it can't save
1415 - Not allowed to return a result set from a function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Davashchenevquestion, 2020-05-09
@nezabor

the issue was resolved by passing the value to the variable

CREATE DEFINER=`root`@`%` FUNCTION `GetMailAcc`(`in_domen_id` int) RETURNS int(11)
BEGIN

DECLARE result_str, count_str INT(11);

  SET count_str = (
      SELECT COUNT(1) AS QTY  
      FROM L_mail_account AS MA 
      WHERE MA.domen_id = in_domen_id 
      ORDER BY last_upd DESC 
      LIMIT 1
    );

  IF count_str > 0 THEN
    SET result_str = (SELECT MA.id FROM L_mail_account AS MA WHERE MA.domen_id = in_domen_id LIMIT 1);
  ELSE
    SET result_str = (SELECT id FROM L_mail_account ORDER BY last_upd DESC LIMIT 1);
  END IF;


  RETURN result_str;
END

but another question remains (((
How to fix update +1 in a function?

K
kisaa, 2020-05-09
@kisaa

https://stackoverflow.com/questions/11880306/why-m...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question