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 ;
3
  • I've fixed the delimiter, but now I get this error, here it in full. #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 Commented Jan 10, 2013 at 3:32
  • you need to use SET, see below. Commented Jan 10, 2013 at 3:34
  • thank you so much, I updated the code above to show you what I have used and as you can see, I had used SET. The problem was no semi-colon after the -1. Thanks again for your help mate! Commented Jan 10, 2013 at 3:44

1 Answer 1

1
  • change the DELIMITER
  • use SET

query,

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 ;
Sign up to request clarification or add additional context in comments.

2 Comments

this worked great, thanks. but now I have another! error, error soup. #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 'else set t_new_id = (SELECT audit_id FROM a_audit_reg WHERE p' at line 12
then you try to run the code above? may be you lack ; after this statement SET t_new_id = -1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.