java.sql.sqlexception:ORA-00917:缺少逗号

时间:2014-04-16 10:32:52

标签: java sql oracle

对不起,我问了一个非常愚蠢的问题,但我找不到我错过代码中逗号的地方..

sqlStr.append("INSERT INTO DS_GOAL ");
sqlStr.append("(DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID, DS_GOAL_ID, ");
sqlStr.append("DS_DESC, TO_CHAR(DS_PLAN_END_DATE, \"dd/MM/YY\"),");
sqlStr.append("DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, ");
sqlStr.append("DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT, DS_FTE, ");
sqlStr.append("DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER ) ");
sqlStr.append("VALUES ");
sqlStr.append("(?, ?, ?, ?, ?,");
sqlStr.append("?, ?,");
sqlStr.append("?, ?, ?, ?,");
sqlStr.append("?, ?, ?, ?,");
sqlStr.append("?, ?, ?)");
sqlStr_insertGoal = sqlStr.toString();

之后     sqlStr.toString()

控制台显示

 INSERT INTO DS_GOAL (DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID,
 DS_GOAL_ID, 
 DS_DESC, TO_CHAR(DS_PLAN_END_DATE, 'dd/MM/YYYY'), 
 DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, 
 DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT,
 DS_FTE, DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER)
 VALUES (?, ?, ?, ?, ?,?, ?,?, ?, ?, ?,?, ?, ?, ?,?, ?, ?)

编辑完代码后 控制台显示

INSERT INTO DS_GOAL (DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID,
 DS_GOAL_ID, 
 DS_DESC, DS_PLAN_END_DATE, 
 DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, 
 DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT,
 DS_FTE, DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER)
 VALUES (?, ?, ?, ?, ?,?, TO_CHAR(DS_PLAN_END_DATE, 'dd/MM/YYYY'),?, ?, ?, ?,?, ?, ?,
 ?,?, ?, ?)

但是控制台显示无效的列索引错误 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我怀疑你的问题实际上并不是一个缺少逗号的情况(根据我的经验,ORA错误因告诉你错误而臭名昭着)。我怀疑您的真正问题是在"电话中围绕格式字符串使用TO_CHAR。为了演示,试试这个:

SELECT TO_CHAR(SYSDATE, "dd/MM/YY")
  FROM DUAL;

如果我执行上述操作,则会出现ORA-00904: "dd/MM/YY": invalid identifier错误。如果我将引号更改为撇号:

SELECT TO_CHAR(SYSDATE, 'dd/MM/YY')
  FROM DUAL;

我得到16/04/14。双引号用于标识符,而不是字符串:

SELECT TO_CHAR(SYSDATE, 'dd/MM/YY') AS "The Date"
  FROM DUAL;                        // ^ This is an identifier

打印:

The Date
--------
16/04/14

编辑:

对不起,我应该早点发现这个!您已在列列表中使用TO_CHAR,但您无法执行此操作。以下示例很好地产生ORA-00917: missing comma错误:

CREATE TABLE JON_TEST (COL1 VARCHAR2(20));

COMMIT;

INSERT INTO JON_TEST (TO_CHAR(COL1, 'DD/MM/YYYY'))
VALUES (SYSDATE);

虽然这有效:

INSERT INTO JON_TEST (COL1)
VALUES (TO_CHAR(SYSDATE, 'dd/MM/YYYY'));

所以你需要纠正三个的事情:

  • 您需要将TO_CHAR更改为TO_DATE
  • 您需要将TO_DATE的呼叫转移到VALUES子句和
  • 您需要确保使用格式字符串'代替"

这就是Oracle define the syntax for INSERT statements

的方式

This is how Oracle defines the syntax for INSERT statements

请注意,在中间部分,它只显示column_name而不是sql_expression

尝试将您的查询更改为以下内容:

sqlStr.append("INSERT INTO DS_GOAL ")
    .append("(DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID, DS_GOAL_ID, ")
    .append("DS_DESC, DS_PLAN_END_DATE, ")
    .append("DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, ")
    .append("DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT, DS_FTE, ")
    .append("DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER ) ")
    .append("VALUES ")
    .append("(?, ?, ?, ?, ?,")
    .append("?, TO_DATE(?, 'dd/MM/YY'),")
    .append("?, ?, ?, ?,")
    .append("?, ?, ?, ?,")
    .append("?, ?, ?)");
    sqlStr_insertGoal = sqlStr.toString();