Create Stored Procedure from File

别来无恙 提交于 2020-01-25 04:06:19

问题


I am trying to execute a whole directory of .SQL files in Java.

I'm struggling to execute a stored procedure. I have found this so far (the most helpful) including a dead link, sadly. I have downloaded liquibase also, but I cannot figure out how I should use it for this purpose.

In my current code, I split the files including procedures into different statements:

(Statements split in a Vector[String] and executed in a for loop)

Example:

//File f;
//Statement st;
Vector<String> vProcedure = getProcedureStatements(f, Charset.defaultCharset(), "//");
for (Iterator<String> itr = vProcedure.iterator(); itr.hasNext();)
    st.execute(itr.next());
System.out.println(f.getName() + " - done executing.");

The Vector contains the four elements (see SQL-Code #SPLIT x).

DROP PROCEDURE IF EXISTS `Add_Position`; #SPLIT 1
DELIMITER // #SPLIT 2
CREATE PROCEDURE `Add_Position`
(
    IN iO_ID INT,
    IN iCID INT,
    IN iWID INT,
    IN iA INT
)
BEGIN
    #statements;
END
// #SPLIT  3
DELIMITER ; #SPLIT 4

Result when trying to execute #SPLIT 2:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'DELIMITER //' at line 1

Q: Could anyone tell me if there's a exteral library I could use, or how liquibase does work ? I can't get it to work the JDBC-way.


回答1:


The statement DELIMITER \\ is not actually SQL - it is a command that is interpreted by the mysql command-line tool (and possibly also their GUI tool), but if you pass it straight to the execution engine, you will get this syntax error.

Unfortunately, Liquibase uses ; as the default statement separator, so it is difficult to get this sort of thing to work using SQL files and Liquibase. I have put in a pull request to allow setting the delimiter from the Liquibase command line - see https://github.com/liquibase/liquibase/pull/361.



来源:https://stackoverflow.com/questions/28781769/create-stored-procedure-from-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!