MySql存储过程out id返回null

时间:2019-01-23 05:15:03

标签: mysql sql

我有一个存储过程,插入到地址表中,它具有3 in参数和1 out参数。如果我调用此过程,则parametr为null

CREATE DEFINER=`root`@`localhost` 
PROCEDURE `insert_adress`(
                        in d varchar(50),
                        in r varchar(50),
                        in c varchar(50),
                        in adress_text varchar(255),
                        out id int)
BEGIN
    declare disc_id int;
    declare reg_id int;
    declare coun_id int;


if ((select count(*) from district where district.dist like d) <> 0)
    then set disc_id := (select id from district where district.dist like d);
    else
        insert into district(dist) values(d);
        set disc_id := (select max(id) from district);
end if;

if ((select count(*) from region where region.reg like r) <> 0)
    then set reg_id := (select id from region where region.reg like r);
    else
        insert into region(reg) values(r);
        set reg_id := (select max(id) from region);
end if;

if ((select count(*) from country where country.coun like c) <> 0)
    then set coun_id := (select id from country where country.coun like c);
    else
        insert into country(coun) values(c);
        set coun_id := (select max(id) from country);
end if;

insert into adress(district,region,country,adress_text) values(disc_id,reg_id,coun_id,adress_text);
set id := (select max(id) from adress);   
END

我称之为:

set @id = 0;
call mustaqil.insert_adress('test1', 'test1', 'test1', 'test1', @id);
select @id;

区,地区,国家/地区表具有“ test1”行 我除外,id,disc_id,reg_id,coun_id应该是数字,但它为null P.S:存储过程插入行

1 个答案:

答案 0 :(得分:0)

只需更改set id:=(从地址中选择max(id)); 至 从地址中选择max(id)到id中; 它应该工作