Last_insert_id与MySQL存储过程一起返回NULL

时间:2018-10-03 15:00:35

标签: c# mysql-workbench

运行存储过程时出现此问题,LAST_INSERT_ID()返回NULL。应该在表行中填充LAST_INSERT_ID()。

我不知道怎么了。

代码如下:

CREATE DEFINER=`root`@`localhost` PROCEDURE `teststudprofile`(IN idno varchar(45),
IN birthdate date,
IN disablty varchar(45),
IN emailaddr varchar(45),
IN contactinfo varchar(45),
IN householdno varchar(45),
IN capitaincome decimal(18,1),
IN studfullname varchar(100),
IN studlname varchar(45),
IN studfname varchar(45),
IN studmname varchar(45),
IN sex int,
IN fathfullname varchar(100),
IN fathlname varchar(45),
IN fathfname varchar(45),
IN fathmname varchar(45),
IN modfullname varchar(100),
IN modlname varchar(45),
IN modfname varchar(45),
IN modmname varchar(45),
IN prescompaddr varchar(100),
IN presbrgy varchar(45),
IN prescitmun varchar(45),
IN presprov varchar(45),
IN preszipcode int,
IN permcompaddr varchar(100),
IN permbrgy varchar(45),
IN permcitmun varchar(45),
IN permprov varchar(45),
IN permzipcode int)
BEGIN
    declare checkstudname int;
    declare checkdisblty int;
    declare checkfathname int;
    declare checkmodname int;
    declare checkpresadd int;
    declare checkpermadd int;
    declare id int;
    declare studnameid int;
    declare studdisbltyid int;
    declare fathnameid int;
    declare mothnameid int;
    declare presaddid int;
    declare permaddid int;
    set checkstudname = 0;
    set checkdisblty = 0;
    set checkfathname = 0;
    set checkmodname = 0;
    set checkpresadd = 0;
    set checkpermadd = 0;


select count(*) into checkstudname from personnames where fullname=studfullname;
        if (checkstudname > 0) then
            select id into studnameid from personnames where fullname=studfullname;
        else
            insert into personnames (id,fullname,lname,fname,mname,sexatbirth,sex) values(id,studfullname,studlname,studfname,studmname,sex,sex);
            select studnameid = LAST_INSERT_ID();
        end if;


    select count(*) into checkdisblty from listdisablties where disablty=disablty;
    if (checkdisblty > 0) then
        select id into studdisbltyid from listdisablties where disablty=disablty;
    else
        insert into listdisablties (id,disablty,Descipt) values(id,disablty,'');
        select studdisbltyid = LAST_INSERT_ID();
    end if;


    select count(*) into checkfathname from personnames where fullname=fathfullname;
    if (checkfathname > 0) then
        select id into fathnameid from personnames where fullname=fathfullname;
    else
        insert into personnames (id,fullname,lname,fname,mname,sexatbirth,sex) values(id,fathfullname,fathlname,fathfname,fathmname,1,1);
        select fathnameid = LAST_INSERT_ID();
    end if;


    select count(*) into checkmodname from personnames where fullname=modfullname;
    if (checkmodname > 0) then
        select id into mothnameid from personnames where fullname=modfullname;
    else
        insert into personnames (id,fullname,lname,fname,mname,sexatbirth,sex) values(id,modfullname,modlname,modfname,modmname,2,2);
        select mothnameid = LAST_INSERT_ID();
    end if;


    select count(*) into checkpresadd from personaddresses where completeaddr=prescompaddr;
    if (checkpresadd > 0) then
        select addrid into presaddid from personaddresses where completeaddr=prescompaddr;
    else
        insert into personaddresses (addrid,completeaddr,strtbrgy,citmun,prov,zipcode) values(id,prescompaddr,presbrgy,prescitmun,presprov,preszipcode);
        select presaddid = LAST_INSERT_ID();
    end if;


    select count(*) into checkpermadd from personaddresses where completeaddr=permcompaddr;
    if (checkpermadd > 0) then
        select addrid into permaddid from personaddresses where completeaddr=permcompaddr;
    else
        insert into personaddresses (addrid,completeaddr,strtbrgy,citmun,prov,zipcode) values(id,permcompaddr,permbrgy,permcitmun,permprov,permzipcode);
        select permaddid = LAST_INSERT_ID();
    end if;

    insert into personprofiles values(id,studnameid,idno,birthdate,studdisbltyid,emailaddr,contactinfo,fathnameid,mothnameid,presaddid,permaddid,householdno,capitaincome);
END

我正在为此数据库使用MySQL工作台。我需要您的帮助以解决我的问题。请帮忙。

0 个答案:

没有答案