2

我创建了下表:

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)
4

1 回答 1

1

正如 OldProgrammer 指出的那样,您的表有 12 列,而您的插入仅填充其中的 7 列。您要么需要为所有列提供值,要么指定要填充的列 - 即使您要填充所有内容,这也会更好,因为它可以更清楚地了解正在发生的事情并避免列排序的潜在问题。

所以看起来你想做:

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,其他四列将作为空值插入。

于 2015-08-17T16:48:12.280 回答