MariaDB 存储过程中的意外 return 值

Unexpected return value on MariaDB stored procedure

这是序列 table:

app app_dt seq_id
ABC 2021-05-11 2

这是我的存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GEN_APPL_NO`(
    in appType varchar(3),
    out applNo varchar(20)
)
begin
    declare prefix varchar(1) default 'E';
    declare seqNo int;
    declare seqLen int default 3;
    declare formNo varchar(20);
    
    select seq_id into  @seqNo  from seq_table
        where app = @appType and app_dt = curdate();

    if( @seqNo is null  )
    then
        set @seqNo = 1;
        insert into seq_table values ( @app , curdate(), @seqNo );
    else 
        update seq_table set seq_id= @seqNo +1 where app= @app and app_dt= curdate();
    end if;
    
    select @prefix || @appType || date_format(curdate(),'%Y%m%d') || lpad(@seqNo,@seqLen,0) into  applNo ;

end

预期结果是 EABC20210511002,但调用后

CALL GEN_EFORM_NO('ABC',?) 

它returns null没有错误。

None 个变量前面应该有一个@;这将使他们尝试访问全局范围内的变量,而不是过程中的参数和局部声明的变量。这适用于您的整个过程,应该如下所示:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GEN_APPL_NO`(
    in appType varchar(3),
    out applNo varchar(20)
)
begin
    declare prefix varchar(1) default 'E';
    declare seqNo int;
    declare seqLen int default 3;
    declare formNo varchar(20);
    
    select seq_id into  seqNo  from seq_table
        where app = appType and app_dt = curdate();

    if( seqNo is null  )
    then
        set seqNo = 1;
        insert into seq_table values ( app , curdate(), seqNo );
    else 
        update seq_table set seq_id= seqNo +1 where app= app and app_dt= curdate();
    end if;
    
    select prefix || appType || date_format(curdate(),'%Y%m%d') || lpad(seqNo,seqLen,0) into  applNo ;

end

请注意,在您的 insertupdate 语句中,您正试图访问未在任何地方定义的 app;你是说 appType 吗?

注意我还假设您启用了 SQL 模式 [PIPES_AS_CONCAT],否则您将需要使用 CONCAT:

select concat(prefix, appType, date_format(curdate(),'%Y%m%d'), lpad(seqNo,seqLen,0)) into  applNo;