我编写了mysql函数,它在Toad / MySQL workbench / Heidi中工作正常。但它提供了以下语法错误
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'CREATE FUNCTION FindAcid(cust_id_in VARCHAR(20))附近使用正确的语法。返回VARCHAR(20)BEGIN DEC'在第1行
以下是我的查询示例:
DROP FUNCTION IF EXISTS FindAcid;
CREATE FUNCTION FindAcid ( cust_id_in VARCHAR(20) )
RETURNS VARCHAR(20)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE div_val VARCHAR(128);
DECLARE for_acid VARCHAR(16);
DECLARE divval VARCHAR(128);
DECLARE rtnforacid VARCHAR(16);
DECLARE scheme_type VARCHAR(10);
DECLARE rtn VARCHAR(10);
DECLARE cur CURSOR FOR
SELECT (appDiv.clr_bal_amt_lkr /(SELECT SUM(COALESCE(appSum.CLR_BAL_AMT_LKR, 0)) AS PORTPOLIO FROM app_dms_daily appSum WHERE appSum.cust_id = appDiv.cust_id GROUP BY appDiv.cust_id )) AS DIV_VAL, schm_type AS SCHM_TYPE, FORACID AS FORACID
FROM app_dms_daily appDiv
WHERE appDiv.cust_id = cust_id_in
ORDER BY DIV_VAL DESC;
DECLARE cur_acid CURSOR FOR
SELECT DISTINCT(COALESCE(foracid, '--')) AS EE
FROM app_dms_daily
WHERE CLR_BAL_AMT_LKR = (SELECT MAX(CLR_BAL_AMT_LKR) FROM app_dms_daily maxapp WHERE maxapp.CUST_ID = cust_id_in AND maxapp.SCHM_TYPE = 'ODA');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
OPEN cur_acid;
read_loop: LOOP
FETCH cur INTO div_val, scheme_type, for_acid;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
if div_val >= 0.3 AND scheme_type = 'ODA' then
read_loop_acid: LOOP
FETCH cur_acid INTO rtn;
IF done THEN
LEAVE read_loop_acid;
END IF;
END LOOP;
SET rtnforacid = rtn;
end if;
CLOSE cur;
CLOSE cur_acid;
RETURN rtnforacid;
END;
SELECT FindAcid('300315759');
这是我的java代码
String query ="DROP FUNCTION IF EXISTS FindAcid; "
+ "CREATE FUNCTION FindAcid ( cust_id_in VARCHAR(20) ) "
+ "RETURNS VARCHAR(20) "
+ "BEGIN "
+ "DECLARE done INT DEFAULT FALSE; "
+ "DECLARE div_val VARCHAR(128); "
+ "DECLARE for_acid VARCHAR(16); "
+ "DECLARE divval VARCHAR(128); "
+ "DECLARE rtnforacid VARCHAR(16); "
+ "DECLARE scheme_type VARCHAR(10); "
+ "DECLARE rtn VARCHAR(10); "
+ "DECLARE cur CURSOR FOR "
+ "SELECT (appDiv.clr_bal_amt_lkr /(SELECT SUM(COALESCE(appSum.CLR_BAL_AMT_LKR, 0)) AS PORTPOLIO FROM app_dms_daily appSum WHERE appSum.cust_id = appDiv.cust_id GROUP BY appDiv.cust_id )) AS DIV_VAL, schm_type AS SCHM_TYPE, FORACID AS FORACID "
+ "FROM app_dms_daily appDiv "
+ "WHERE appDiv.cust_id = cust_id_in "
+ "ORDER BY DIV_VAL DESC; "
+ "DECLARE cur_acid CURSOR FOR "
+ "SELECT DISTINCT(COALESCE(foracid, '--')) AS EE "
+ "FROM app_dms_daily "
+ "WHERE CLR_BAL_AMT_LKR = (SELECT MAX(CLR_BAL_AMT_LKR) FROM app_dms_daily maxapp WHERE maxapp.CUST_ID = cust_id_in AND maxapp.SCHM_TYPE = 'ODA'); "
+ "DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; "
+ "OPEN cur; "
+ "OPEN cur_acid; "
+ "read_loop: LOOP "
+ "FETCH cur INTO div_val, scheme_type, for_acid; "
+ "IF done THEN "
+ "LEAVE read_loop; "
+ "END IF; "
+ "END LOOP; "
+ "if div_val >= 0.3 AND scheme_type = 'ODA' then "
+ "read_loop_acid: LOOP "
+ "FETCH cur_acid INTO rtn; "
+ "IF done THEN "
+ "LEAVE read_loop_acid; "
+ "END IF; "
+ "END LOOP; "
+ "SET rtnforacid = rtn; "
+ "end if; "
+ "CLOSE cur; "
+ "CLOSE cur_acid; "
+ "RETURN rtnforacid; "
+ "END;";
getJdbcTemplate().execute(query);
答案 0 :(得分:0)
你没有粘贴Java代码,所以我在你的异常中猜测你在使用sql创建字符串时做了类似的事情:
"CREATE FUNCTION FindAcid ( cust_id_in VARCHAR(20) )" +
"RETURNS VARCHAR(20)" +
"BEGIN" + ...
你必须在行尾添加空格字符:
"CREATE FUNCTION FindAcid ( cust_id_in VARCHAR(20) ) " +
"RETURNS VARCHAR(20) " +
"BEGIN " + ...