我有这个程序:
CREATE OR REPLACE PROCEDURE LOG_TEST
( IN QUERY VARCHAR(24576),
IN LOGTBL_VAR VARCHAR(20) )
LANGUAGE SQL
SPECIFIC LOG_TEST
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_INS_STRING VARCHAR(1024);
DECLARE STMT STATEMENT;
DECLARE STMT_INS STATEMENT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS
EXCEPTION
1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE;
END;
SET V_QUERY = QUERY;
PREPARE STMT FROM V_QUERY;
EXECUTE STMT;
SET V_QUERY = REPLACE(V_QUERY,'''', '''''');
SET V_INS_STRING= 'INSERT INTO '||LOGTBL_VAR||'(QUERY, SQL_CODE, SQL_STATE, ERROR_MSG, BEZUG, TIME) VALUES ('''||V_QUERY||''', '||V_SQLCODE||', '''||V_SQLSTATE||''', '''||V_ERRORMSG||''')';
PREPARE STMT_INS FROM V_INS_STRING;
EXECUTE STMT_INS;
END@
该程序运行良好,一切都很好,但只有一件事。如果通过参数给出的查询无效,那么我总是会得到SQLCODE -727和SQLSTATE 56098,这是该过程本身的一般错误,但是我需要查询所引起的SQLCODE和SQLSTATE,这样我才能看到问题出在哪里查询本身。我该怎么办?
我在Windows v10.5上运行DB2
有任何想法吗?
谢谢。
最佳答案
您的INSERT语句不正确-列数(6)和值(4)不同。
您究竟在LOG_TEST表中得到什么?
CREATE OR REPLACE PROCEDURE LOG_TEST (
IN QUERY VARCHAR(24576)
, IN LOGTBL_VAR VARCHAR(20)
)
SPECIFIC LOG_TEST
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS EXCEPTION 1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE) INTO V_SQLSTATE, V_SQLCODE;
END;
EXECUTE IMMEDIATE QUERY;
PREPARE STMT_INS FROM 'INSERT INTO '||LOGTBL_VAR||'(QUERY, SQL_CODE, SQL_STATE, ERROR_MSG) VALUES (?,?,?,?)';
EXECUTE STMT_INS USING QUERY, V_SQLCODE, V_SQLSTATE, V_ERRORMSG;
END@
CREATE TABLE LOG_TEST(QUERY VARCHAR(1000), SQL_CODE INT, SQL_STATE CHAR(5), ERROR_MSG VARCHAR(1000)) IN USERSPACE1@
CALL LOG_TEST('DELETE FROM LOG_TEST - WRONG QUERY', 'LOG_TEST')@
SELECT * FROM LOG_TEST@
如果要运行SELECT语句并获取结果,则:
CREATE OR REPLACE PROCEDURE LOG_TEST (
IN QUERY VARCHAR(24576)
, IN LOGTBL_VAR VARCHAR(20)
, OUT RES INT
)
SPECIFIC LOG_TEST
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS EXCEPTION 1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE) INTO V_SQLSTATE, V_SQLCODE;
END;
SET V_QUERY='SET (?) = ('||REPLACE(QUERY, '''', '''''')||')';
PREPARE STMT_SEL FROM V_QUERY;
EXECUTE STMT_SEL INTO RES;
PREPARE STMT_INS FROM 'INSERT INTO '||LOGTBL_VAR||'(QUERY, SQL_CODE, SQL_STATE, ERROR_MSG) VALUES (?,?,?,?)';
EXECUTE STMT_INS USING QUERY, V_SQLCODE, V_SQLSTATE, V_ERRORMSG;
END@
关于sql - 从过程DB2中的内部SQL调用获取SQLCODE和SQLSTATE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53334182/