2013-08-21 120 views
0

我戰鬥是從停止我的存儲過程中的錯誤工作:SQL插入使用存儲過程

「您的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; 

我在做什麼不正確,爲什麼我的比較工作不會?謝謝!


UPDATE: 我的問題是不是一個IF /然後進行比較。這是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是:「遊標‘遊標名’不能按照規定使用,因爲在申報命名的預備語句遊標不是一個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; 
+0

後,你必須運行在一個觸發器或過程的代碼。 –

+0

你可以發佈與此相關的SQL的其餘部分嗎?動態SQL將需要在存儲過程中進行處理(或者可能是觸發器)。 –

+0

您的錯誤消息表明您正在使用MySQL,您爲什麼要標記[Teradata]? – dnoeth

回答

0

我回答我的問題。我刪除光標,並增加了執行的語句

EXECUTE PREPSTMT; 

的準備

相關問題