java.sql.SQLSyntaxErrorException:ORA-00947:插入语句中的值不够

时间:2015-08-17 16:20:23

标签: sql oracle sql-merge

我创建了以下表格:

BEGIN
  EXECUTE IMMEDIATE 'CREATE TABLE "MY2XSYEHT" (
    pKy NUMBER(19, 0) PRIMARY KEY AUTOINCREMENT,
    cTm NUMBER(19, 0), uTm NUMBER(19, 0), oTm NUMBER(19, 0), eTm NUMBER(19, 0),
    oID VARCHAR2(32), kID VARCHAR2(32), idx NUMBER(3,0), typ NUMBER(3,0),
    nVl FLOAT(24), sVl VARCHAR2(64), tVl VARCHAR2(4000) )';
  EXCEPTION WHEN OTHERS THEN
    IF SQLCODE != -955 THEN RAISE;
  END IF;
END; 

在主键上创建序列和触发器:

CREATE SEQUENCE "MY2XSYEHT_SEQ" START WITH 1001 INCREMENT BY 1 CACHE 10

CREATE OR REPLACE TRIGGER "MY2XSYEHT_TRIGGER"
BEFORE INSERT ON "MY2XSYEHT"
FOR EACH ROW
DECLARE
BEGIN
  IF( :NEW.pKy IS NULL ) THEN 
    :NEW.pKy := "MY2XSYEHT_SEQ".nextval
  END IF;
END;

执行以下MERGE语句时,我收到“ORA-00947:值不够”

MERGE INTO "MY2XSYEHT" destTable
USING (SELECT ? oID, ? kID, ? idx, ? typ, ? nVl, ? sVl, ? tVl FROM DUAL) srcTable
ON (destTable.oID=srcTable.oID
  and destTable.kID=srcTable.kID
  and destTable.idx=srcTable.idx)
WHEN MATCHED THEN UPDATE SET destTable.typ=srcTable.typ,
  destTable.nVl=srcTable.nVl,
  destTable.sVl=srcTable.sVl,
  destTable.tVl=srcTable.tVl
WHEN NOT MATCHED THEN INSERT VALUES (srcTable.oID, srcTable.kID, srcTable.idx,
  srcTable.typ, srcTable.nVl, srcTable.sVl, srcTable.tVl)

1 个答案:

答案 0 :(得分:1)

正如OldProgrammer指出的那样,你的表有12列,你的插入只填充其中的7列。您需要为所有列提供值,或者指定要填充的列 - 即使您填充所有列也要好得多,因为它可以更清楚地发生了什么,并避免了列排序的潜在问题。 / p>

所以看起来你想要这样做:

WHEN NOT MATCHED THEN INSERT (oID, kID, idx, typ, nVl, sVl, tVl)
  VALUES (srcTable.oID, srcTable.kID, srcTable.idx,
    srcTable.typ, srcTable.nVl, srcTable.sVl, srcTable.tVl)

您的触发器将自动填充pKy,其他四列将插入为空。