将UPSERT插入表格的任何更好的方法,都提供:
以下过程抛出:“ ORA-00942:表或视图不存在”
CREATE OR REPLACE PROCEDURE PROCEDURE "SPINSERTDATA" ( pObjectID IN RAW, pDateTime IN TIMESTAMP, pValue IN BINARY_DOUBLE, ) AS BEGIN Declare vQueryInsert VARCHAR2(1000); vQueryUpdate VARCHAR2(1000); vTableName VARCHAR2(30); Begin vTableName := FGETTABLENAME(POBJECTID => pObjectID); vQueryUpdate := 'UPDATE ' || vTableName || ' SET "VALUE" = :1'; vQueryInsert := 'INSERT INTO ' || vTableName || ' ("DTTIME", "VALUE") VALUES (:1, :2)'; EXECUTE IMMEDIATE vQueryInsert USING pDateTime, pValue; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN EXECUTE IMMEDIATE vQueryUpdate USING pValue; End; END "SPINSERTDATA";
MERGE与本机动态SQL完美配合(立即执行):
create table so_test(pk number not null primary key, value varchar2(20)); insert into so_test(pk, value) values(1, 'one'); declare l_SQL varchar2(4000); l_tablename varchar2(4000) default 'so_test'; begin l_SQL := 'merge into ' || l_tablename || ' target' || ' using (select 1 pk, ''eins'' value from dual union all select 2 pk, ''zwei'' value from dual) source on (target.pk = source.pk) when matched then update set target.value = source.value when not matched then insert values(source.pk, source.value) '; dbms_output.put_line(l_sql); execute immediate l_SQL; end;
您能否发布使用MERGE时收到的错误消息?