1

I'm trying to build a mysql function which inserts values into a table and returns me the last_id inserted, which should be the same after executing the insertion. But Xampp gives me an error in the line where im declaring the "last_bill_id" variable. Can someone please help me to understand what am I doing wrong?

Here is the code for the function:

CREATE FUNCTION insert_bill(

        client_id varchar (12),
        bill_date date
    ) RETURNS INT
    BEGIN
        DECLARE last_bill INT; 
        INSERT INTO bill
        (
            client_id, bill_date
        )
         VALUES
        (
            client_id, bill_date
        );    
        SET last_bill = LAST_INSERT_ID();
        RETURN last_bill;  
    END $$
    DELIMITER ;

Error: #1064 - Something is wrong about 'INT

1 Answer 1

2

you need another DELIMITER at the start

DELiMiTER $$
CREATE FUNCTION insert_bill(

    client_id varchar (12),
    bill_date date
) RETURNS INT
DETERMINISTiC
BEGIN
    DECLARE last_bill INT; 
    INSERT INTO bill
    (
        client_id, bill_date
    )
     VALUES
    (
        client_id, bill_date
    );    
    SET last_bill := LAST_INSERT_ID();
    RETURN last_bill;  
END $$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot , the function got created, but when I execute this: SELECT insert_bill('111111111', STR_TO_DATE('18-03-2020', '%d-%m-%Y'); it gives me the same error
I removed the "DETERMINISTIC" and it worked, for my query: SELECT insert_bill2('111111111', STR_TO_DATE('14-03-2020', '%d-%m-%Y'))

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.