Why does this SQL stored procedure require that a temp table be created for it to work (return results)?

蹲街弑〆低调 提交于 2019-12-05 19:36:27

What you want to say is this:

CREATE PROCEDURE build_jics_user (pid INT)
    RETURNING CHAR(8);
    DEFINE username CHAR(8);
    SELECT LOWER((SUBSTR(firstname,0,1))||(SUBSTR(lastname,0,7))) INTO username
      FROM id_rec
      WHERE id = pid;
    RETURN username;
END PROCEDURE;

... and execute it like this:

EXECUTE PROCEDURE build_jics_user(42);

UPDATE

If the purpose of this is to be a function, where it's required inside some other SQL, then you might do the following:

CREATE FUNCTION jics_user(fname VARCHAR(255), lname VARCHAR(255))
    RETURNING CHAR(8);
    RETURN LOWER(SUBSTR(fname,0,1) || SUBSTR(lname,0,7));
END FUNCTION;

... and execute it like this:

SELECT id, firstname, lastname, jics_user(firstname, lastname) AS jics_user, ...
  FROM id_rec;

There's no real technical difference between a PROCEDURE and a FUNCTION, it's more an assertion as to how it's used.

This seems to be per design (which must be accounting for the absence of the 'similarly simple examples online'). Apparently, whatever data you are pulling with a SELECT statement in a stored procedure, you cannot return them directly. You should store them either in a temporary table or in variables for later use.

It is likely that your SELECT statement should look like this

SELECT LOWER((SUBSTR(firstname,0,1))||(SUBSTR(lastname,0,7))) INTO username
FROM id_rec
WHERE id = pid;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!