我正在与一个错误作斗争,该错误使我的存储过程停止工作:
“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'IF (SELECTED_PROJECT_LOBSELECTION = 'Copy the Whole Project') THEN SET lob' 附近使用正确的语法:”
这是我的 IF/ELSEIF 语句的一部分:
IF (SELECTED_PROJECT_LOBSELECTION = 'Copy the Whole Project') THEN
SET lobInsertStmt = ' AND p.PROJECTION_PROJECT_NME = ' || SELECTED_PROJECT_NAME
|| ' AND p.PROJECTION_PROJ_CATG_NME = ' || SELECTED_PROJECT_CATG
|| ' AND p.FINANCIAL_PROJECTION_YEAR_NUM = ' || CAST(SELECTED_PROJECT_YEAR AS int)
|| ' AND g.MODEL_GROUP_SNAPSHOT_TSP = ' || SELECTED_SNAPSHOT_TIMESTAMP;
我做错了什么,为什么我的比较不起作用?谢谢!
更新: 我的问题不是 IF/THEN 比较。这就是读取 IN 参数的方式。这是解决方案:
IF (SELECTED_PROJECT_LOBSELECTION = 'Copy the Whole Project') THEN
SET lobInsertStmt = ' AND p.PROJECTION_PROJECT_NME = ''' || SELECTED_PROJECT_NAME || ''''
|| ' AND p.PROJECTION_PROJ_CATG_NME = ''' || SELECTED_PROJECT_CATG || ''''
|| ' AND p.FINANCIAL_PROJECTION_YEAR_NUM = ' || TRIM(CAST(SELECTED_PROJECT_YEAR AS int))
|| ' AND g.MODEL_GROUP_SNAPSHOT_TSP = ''' || SELECTED_SNAPSHOT_TIMESTAMP || '''';
现在,我收到 07005 的 SQLSTATE,即:“游标 'cursor-name' 不能按指定使用,因为游标声明中命名的预准备语句不是 SELECT 语句。”
如何在没有游标的情况下使用存储过程进行插入?
这是附加代码:
REPLACE PROCEDURE "SCHEMA"."PROCEDURE_NAME" (
IN "SELECTED_PROJECT_NAME" VARCHAR(256) CHARACTER SET LATIN,
IN "SELECTED_PROJECT_CATG" VARCHAR(256) CHARACTER SET LATIN,
IN "SELECTED_PROJECT_YEAR" VARCHAR(4) CHARACTER SET LATIN,
IN "SELECTED_SNAPSHOT_TIMESTAMP" VARCHAR(28) CHARACTER SET LATIN,
IN "SELECTED_PROJECT_TYPE" VARCHAR(6) CHARACTER SET LATIN,
IN "SELECTED_PROJECT_LOBSELECTION" VARCHAR(256) CHARACTER SET LATIN,
IN "SELECTED_LOB_NUMBERS" VARCHAR(256) CHARACTER SET LATIN,
IN "COPY_TO_PROJECT_NAME" VARCHAR(256) CHARACTER SET LATIN,
IN "REQUESTER_ID" VARCHAR(8) CHARACTER SET LATIN,
OUT "TEST_OUT" VARCHAR(5000) CHARACTER SET LATIN,
OUT "SQLSTATE_OUT" VARCHAR(5) CHARACTER SET LATIN,
OUT "SQLCODE_OUT" INTEGER)
DYNAMIC RESULT SETS 1
P1: BEGIN
DECLARE lobInsertStmt varchar(500);
DECLARE mystmt varchar(5000);
DECLARE CURSOR_C CURSOR WITH RETURN FOR PREPSTMT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET SQLCODE_OUT = SQLCODE;
SET SQLSTATE_OUT = SQLSTATE;
SET TEST_OUT = mystmt;
END;
IF (SELECTED_PROJECT_LOBSELECTION = 'Copy the Whole Project') THEN
SET lobInsertStmt = ' AND p.PROJECTION_PROJECT_NME = ''' || SELECTED_PROJECT_NAME || ''''
|| ' AND p.PROJECTION_PROJ_CATG_NME = ''' || SELECTED_PROJECT_CATG || ''''
|| ' AND p.FINANCIAL_PROJECTION_YEAR_NUM = ' || TRIM(CAST(SELECTED_PROJECT_YEAR AS int))
|| ' AND g.MODEL_GROUP_SNAPSHOT_TSP = ''' || SELECTED_SNAPSHOT_TIMESTAMP || '''';
-- Other ElseIFs
END IF;
SET mystmt = 'INSERT INTO D_FAR_WORK_VW.V_COPY_MODEL_PROCESS_WORK'
|| ' SELECT reqt.RUN_ID,'''
|| SELECTED_PROJECT_NAME || ''' AS TO_PROJECTION_PROJECT_NME,'
|| ' m.PROJECTION_MODEL_SEQ_ID,'
|| ' m.PROJECTION_MODEL_ROW_SEQ_ID,'
|| ' p.PROJECTION_PROJECT_NME as FROM_PROJECTION_PROJECT_NME, '''
-- More insert code
PREPARE PREPSTMT FROM mystmt;
OPEN CURSOR_C;
SET TEST_OUT = mystmt;
SET SQLSTATE_OUT = SQLSTATE;
SET SQLCODE_OUT = SQLCODE;
END P1;