今天在mysql中写的存储过程,运行时总是报错:
SQL state [02000]; error code [1329]; No data - zero rows fetched, selected, or processed; nested exception is java.sql.SQLException: No data - zero rows fetched, selected, or processed
在mysql的文档中查到:
SELECT ... INTO syntax enables selected columns to be stored directly into variables. The query should return a single row. If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query returns multiple rows, error 1172 occurs (Result consisted of more than one row). If it is possible that the statement may retrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.
错误处理:
DECLARE for Handlers
DECLARE handler_type HANDLER
FOR condition_value [, condition_value] ...
statement
handler_type:
CONTINUE
| EXIT
| UNDO
condition_value:
SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| mysql_error_code
修改存储过程如下:
CREATE PROCEDURE aaa_proc(IN inaccount VARCHAR(30),OUT flag int)
BEGIN
DECLARE bankaccountid char(30);
DECLARE continue handler for 1329 set flag=3;--错误处理
SET flag=0;
select bankaccount into bankaccountid from payaccount where accountid=inaccount;
IF bankaccountid is NULL THEN
SET flag = 1;
END IF;
END
SQL state [02000]; error code [1329]; No data - zero rows fetched, selected, or processed; nested exception is java.sql.SQLException: No data - zero rows fetched, selected, or processed
在mysql的文档中查到:
SELECT ... INTO syntax enables selected columns to be stored directly into variables. The query should return a single row. If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query returns multiple rows, error 1172 occurs (Result consisted of more than one row). If it is possible that the statement may retrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.
错误处理:
DECLARE for Handlers
DECLARE handler_type HANDLER
FOR condition_value [, condition_value] ...
statement
handler_type:
CONTINUE
| EXIT
| UNDO
condition_value:
SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| mysql_error_code
修改存储过程如下:
CREATE PROCEDURE aaa_proc(IN inaccount VARCHAR(30),OUT flag int)
BEGIN
DECLARE bankaccountid char(30);
DECLARE continue handler for 1329 set flag=3;--错误处理
SET flag=0;
select bankaccount into bankaccountid from payaccount where accountid=inaccount;
IF bankaccountid is NULL THEN
SET flag = 1;
END IF;
END