0

I'm trying to create a stored procedure in a MySQL database using the contents of a text file:

USE myDatabase;

DROP PROCEDURE IF EXISTS myStoredProcedure;

DELIMITER $$

CREATE PROCEDURE myStoredProcedure
(
    _description VARCHAR(50),
    _value INT
)
BEGIN

    INSERT INTO myTable
    (
        description,
        value
    ) VALUES (
        _description,
        _value
    );

    SELECT 
        id, 
        description, 
        value
    FROM myTable
    WHERE id = LAST_INSERT_ID();

END;

$$

DELIMITER ;

I execute the SQL using a native query:

Query query = entityManager.createNativeQuery(queryText);
...
query.executeUpdate();

But it gets an error on the DROP PROCEDURE I commented out the DROP PROCEDURE and then it gets an error on the DELIMITER Basically, it gets an error on any line after the first semicolon.

It seems as if JPA hibernate is parsing my query and telling me there's a problem with it rather than passing the unadulterated text onto MySQL.

The sql runs in MySQL without error.

I can't find anything in Google about creating a stored procedure with JPA, only calling one.

Does anyone have any insight on what I might be doing wrong? Or if this is even possible.

1 Answer 1

5

This can be possible if you mention the following property in the url

spring.datasource.url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true

The allowMultiQueries will instruct the driver to sent delimited queries to the database.

Please note that if you are using native queries be-aware of sql injection attack. You dont need to put the delimiter(DELIMITER) explicitly.The sql statement The following query works

SET myDatabase; 

DROP PROCEDURE IF EXISTS myStoredProcedure; 

CREATE PROCEDURE myStoredProcedure ( _description VARCHAR(50), _value INT ) 

BEGIN 

INSERT INTO 
    myTable ( description, value ) 
VALUES ( _description, _value ); 

SELECT id, description, value 
  FROM myTable 
WHERE id = LAST_INSERT_ID(); 

END;
Sign up to request clarification or add additional context in comments.

5 Comments

That works right up until it hits DELIMITER $$ Then it flags the DELIMITER as a syntax error.
I assume that because the delimiter doesn't end in a semicolon
You can remove the DELIMITER $$ altogether and use the semi-colon instead coupled with allowMultiQueries=true it should work
The following modified query works USE myDatabase; DROP PROCEDURE IF EXISTS myStoredProcedure; CREATE PROCEDURE myStoredProcedure ( _description VARCHAR(50), _value INT ) BEGIN INSERT INTO myTable ( description, value ) VALUES ( _description, _value ); SELECT id, description, value FROM myTable WHERE id = LAST_INSERT_ID(); END;
Thanks! Normally when I run a create procedure statement in MySQL workbench, it stops the parsing when it hits the semicolon, this obviously works differently.

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.