2015-06-01 35 views
1

我有以下使用DBACCESS存儲過程:如何在Informix中正確輸入存儲過程參數?

CREATE PROCEDURE proc_1 (p_name VARCHAR(20), 
p_usernum INTEGER) 
RETURNING CHAR(7); 
DEFINE err INTEGER; 

ON EXCEPTION SET err 
RETURN 'ERROR'; 
END EXCEPTION 

ON EXCEPTION IN (150) SET err 
RETURN 'NOTHING'; 
END EXCEPTION 

INSERT INTO database1 (name, usernum,) 
VALUES (p_name, p_usernum,); 

IF DBINFO('sqlca.sqlerrd2') = 0 
     THEN RAISE EXCEPTION 150; 
    END IF; 

RETURN 'YES'; 
END PROCEDURE; 

.... 我得到一個錯誤-201當存儲過程運行。 第一個參數後面的下一行是否爲錯誤? 是否有正確的方法允許參數移動到存儲過程中的下一行? 正在嘗試執行的操作是使用INSERT運行存儲過程,並在INSERT不成功的情況下返回錯誤信息。

+0

您是否使用'dbaccess'或'isql'運行SQL ?使用'isql'將不起作用,因爲它不能識別必須發送多個語句。但是,根據對話判斷,您可能正在使用'dbaccess',然後回答中描述的語法錯誤是您的主要問題。 –

+0

是的,我正在使用dbaccess,查看我的存儲過程我不確定語法在哪裏出現問題... –

+0

Ricardo Henriques建議的代碼適用於我。有一個語法錯誤的地方在你的行'DEFINE err;'中缺少類型信息。既然你從不使用'err'中的值,你可以簡單地省略它和'SET err'子句。一般情況下'ON EXCEPTION'優先於ON EXCEPTION IN(150)'(或IN(746)'),所以你只有'ERROR',而不是'NOTHING'。你也永遠不會得到一個插入一行的成功的INSERT(當你使用VALUES列表時),所以'IF DBINFO('sqlca.sqlerrd2')= 0'條件永遠不會觸發。 –

回答

2

我不知道這是否是一個錯誤的copy+paste但你定義err時失蹤的類型,你必須在INSERT子句一些額外的逗號和你返回CHAR(5)被截斷的價值NOTHING。試試這個:

CREATE PROCEDURE proc_1 (p_name VARCHAR(20), p_usernum INTEGER) RETURNING CHAR(7); 

DEFINE err INTEGER; 

ON EXCEPTION SET err 
    RETURN 'ERROR'; 
END EXCEPTION 

ON EXCEPTION IN (746) SET err 
RETURN 'NOTHING'; 
END EXCEPTION 

INSERT INTO database1 (name, usernum) 
VALUES (p_name, p_usernum); 

IF DBINFO('sqlca.sqlerrd2') = 0 
     THEN RAISE EXCEPTION 746; 
    END IF; 

RETURN 'YES'; 
END PROCEDURE; 

不要使用異常150,你有一個自定義的用於此(746):

[[email protected] ~]$ finderr 150 
-150 The limits of the IBM Informix Demo Version have been exceeded. 

You are using a demonstration version of the database server. This 
version has severe limits on the number of tables and the size of the 
tables that it can manage. The current operation causes it to exceed 
one of those limits. Contact your IBM representative about buying 
the production version of the software. 


[[email protected] ~]$ finderr 746 
-746 message-string 

You supply message-string for this message. You can apply this message 
to error conditions that you specify in an SPL routine. The corrective 
action for this error depends on the condition that caused it. You, the user, 
define both the condition and the message text. 


[[email protected] ~]$ 
+0

因此,自定義錯誤與錯誤代碼746一起工作? –

+0

746是您想要自定義錯誤消息的此類解決方案的預留錯誤代碼。 –

+0

我修改了存儲過程,這似乎對於它的目的是有效的。 –

相關問題