mysql stored function parameter

前端 未结 1 1177
遇见更好的自我
遇见更好的自我 2021-01-19 18:46

I have just started to create a stored function this is my first time so I am having a few problems. Currently I call the function using SELECT test(); (test is

相关标签:
1条回答
  • 2021-01-19 19:25

    Use:

    DROP FUNCTION IF EXISTS `example`.`test` $$
    CREATE FUNCTION `example`.`test` (param INT) RETURNS VARCHAR(32)
    BEGIN
    
      DECLARE new_username VARCHAR(32);
    
        SELECT `username`
          INTO new_username
          FROM `users`
         WHERE `ID` = param;
    
        RETURN COALESCE(new_username, 'Username not found');
    
    END $$
    

    Mind that the VARCHAR length of the RETURN value matches the variable, which should match the column length you want to return.

    0 讨论(0)
提交回复
热议问题