3
#1064 - 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 = -1 else SELECT audit_id INTO t_new_id FROM a_audit_reg at line 11



delimiter //
CREATE  FUNCTION get_audit_id (p_pubco_id int(10),
                              p_audit_id int(10),
                              p_fiscal_date date) 
       RETURNS int(10) 
BEGIN 

   DECLARE t_new_id int(10);

#check paremeters here
if p_pubco_id = 0 or p_audit_id = 0
then set t_new_id = -1
else
   set t_new_id = (SELECT audit_id
     FROM a_audit_reg 
    WHERE p_pubco_id = a_audit_reg.pubco_id
      and p_audit_id = a_audit_reg.audit_id
      and p_fiscal_period = a_audit_reg.fiscal_period_date);

   if found_rows() = 0
   then
        insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
               values (p_pubco_id, p_audit_id, p_fiscal_date);
        set t_new_id = last_insert_id();
   end if;
end if;
   return t_new_id;

END //
delimiter ;
4

1 回答 1

1
  • 改变DELIMITER
  • 利用SET

询问,

DELIMITER //
CREATE  FUNCTION get_audit_id
(
    p_pubco_id INT, 
    p_audit_id INT, 
    p_fiscal_date DATE
) 
RETURNS INT
BEGIN 
    DECLARE t_new_id INT;

    IF p_pubco_id = 0 or p_audit_id = 0 THEN    
        SET t_new_id = -1;
    ELSE
        SET t_new_id = (SELECT audit_id
                        FROM a_audit_reg 
                        WHERE p_pubco_id = a_audit_reg.pubco_id
                            and p_audit_id = a_audit_reg.audit_id
                            and p_fiscal_period = a_audit_reg.fiscal_period_date);

        IF found_rows() = 0 then
            insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
            values (p_pubco_id, p_audit_id, p_fiscal_date);

            SET t_new_id = last_insert_id();
        end if;
    end if;
    return t_new_id;
END //
DELIMITER ;
于 2013-01-10T03:29:16.570 回答