SQL插入使用存储过程

时间:2013-08-21 20:30:21

标签: sql stored-procedures triggers cursor

我正在解决阻止我的存储过程停止工作的错误:

“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'IF(SELECTED_PROJECT_LOBSELECTION ='复制整个项目')附近使用正确的语法。然后在第1行设置高亮显示: “

以下是我的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 params的阅读方式。以下是解决方案:

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 || '''';

现在,我收到的SQLSTATE为07005,它是:“无法按指定使用游标'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;

1 个答案:

答案 0 :(得分:0)

我回答了我的问题。我删除了游标并添加了一个执行语句

EXECUTE PREPSTMT;

准备之后