Skip to content
Advertisement

How to make mysql function that return from the select result?

I want to create this function on mySql 8. It will create a sequence number like 00001,00002

CREATE FUNCTION dbOne.create_sequence_number(lastNumber CHAR(255), numberLength INT, lastValue CHAR(255) ) RETURNS char(255)
BEGIN
    DECLARE select_var CHAR(255);
    SET select_var = (SELECT 
        CASE WHEN lastNumber = lastValue 
        THEN
        LPAD( '1', numberLength, '0' ) 
        ELSE 
        LPAD(CAST(( CAST(COALESCE ( lastNumber, '0' ) AS INT) + 1 ) AS VARCHAR, numberLength, '0' ) INTO select_var);
    RETURN select_var;
END

i dunno whats wrong with this query but i always got this error.

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT) + 1 ) AS VARCHAR, numberLength, '0' ) INTO select_var);
    RETURN select_var' at line 9
  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT) + 1 ) AS VARCHAR, numberLength, '0' ) INTO select_var);
    RETURN select_var' at line 9
  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT) + 1 ) AS VARCHAR, numberLength, '0' ) INTO select_var);
    RETURN select_var' at line 9

I also tried with this query.

CREATE FUNCTION erhav2_db.create_sequence_number(lastNumber CHAR(255), numberLength INT, lastValue CHAR(255) ) RETURNS char(255)
BEGIN
    DECLARE select_var CHAR(255);
    SELECT 
        (CASE WHEN lastNumber = lastValue 
        THEN
        lpad( '1', numberLength, '0' ) 
        ELSE 
        lpad(CAST(( CAST(COALESCE ( lastNumber, '0' ) AS INT) + 1 ) AS VARCHAR, numberLength, '0' ))) INTO select_var;
    RETURN select_var;
END

but still gave me with the same error. What could go wrong with my function query ?

Advertisement

Answer

CREATE FUNCTION dbOne.create_sequence_number(
    lastNumber /* CHAR(255) */ UNSIGNED, 
    numberLength INT, 
    lastValue CHAR(255) 
) 
RETURNS CHAR(255)
RETURN LPAD(CASE WHEN lastNumber = lastValue
                 THEN 1
                 ELSE COALESCE(lastNumber, 0) + 1
                 END,
            numberLength, 
            '0');

Multiple datatype convertions are excess – MySQL will change the datatype implicitly according to the operation context.

All operations can be performed in single statement which makes both declared variable and BEGIN-END (and delimiter reassigning) unnecessary.

The code needs lastNumber to be convertable to numeric datatype. If not then both your and my code will fail in strict SQL mode. So I recommend to change the datatype of lastNumber CHAR(255) input parameter datatype to UNSIGNED / INT – this will allow to detect the value incorrectness on the function call stage, not in the function code.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement