匿名plsql块中的列索引无效

时间:2014-10-06 09:10:54

标签: java oracle plsql

当我尝试设置此查询的参数Invalid column index时,我收到in_cnt_date

public static final String CONTEXT = "DECLARE in_cnt_date DATE := TO_DATE('&'); " +
" hv_cnt_id NUMBER := 0; " +
" BEGIN DBMS_OUTPUT.ENABLE (NULL); " +
" INSERT INTO dt_contexts (CNT_ID, CNT_CONTEXT, CNT_TYPE, CNT_SOURCE, CNT_COMMENT, CNT_DATE, CNT_DATE_INSERT, CNT_DATE_UPDATE) VALUES (0, 'EPE_CONTEXT', 'ROUTE', 'bdd', 'Built from ROUTE', in_cnt_date, SYSDATE, SYSDATE); " +
" SELECT SEQ_DT_CNT_ID.CURRVAL INTO hv_cnt_id FROM DUAL; " +
" dbms_output.put_line(hv_cnt_id); " +
" EXCEPTION WHEN OTHERS THEN RAISE ; END;";

参数设置:

CallableStatement cs = null;
    ResultSet rs = null;
    int contextId = 0;
    try {
        conn.setAutoCommit(false);
        cs = conn.prepareCall(CONTEXT);
        cs.setDate(1, (java.sql.Date) Route.datePrf);

你能帮忙吗?

2 个答案:

答案 0 :(得分:3)

您当前的语句没有任何绑定变量,因此当您尝试绑定索引1时,会出现此错误。绑定变量占位符是一个问号,而不是一个&符号,不应引用:

"DECLARE in_cnt_date DATE := TO_DATE(?); " +

但是您使用Date进行设置,因此您不需要TO_DATE()来电:

"DECLARE in_cnt_date DATE := ?; " +

答案 1 :(得分:0)

您必须输入字符数据类型到TO_DATE函数

请参阅链接here

The TO_DATE function converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of DATE datatype