1

我正在创建一个使用合并语句(upsert)更新/插入表的过程。现在我遇到了一个问题:使用过程参数我必须执行此 upsert。

过程 xyz( a in table.a%type,b in table.b%type,....) 是一些局部变量;开始使用 source_table 合并到 target_table -而不是源表,我必须 在匹配时在(表中主键的条件)上使用过程参数,然后在不匹配时更新表,然后插入表;结束 xyz;那么如何在合并语句中使用过程参数而不是源表?或建议我查询以获取过程参数并将其用作源表值。

请帮帮我。提前致谢。

4

3 回答 3

1

我知道我迟到了八年,但我认为我正在尝试做一些与你正在做的事情类似的事情,但尝试根据传递给存储过程的参数进行 Upsert,该存储过程在成功时返回一个空字符串并且返回我的 VB 代码时出现错误。下面是我的所有代码以及解释我做了什么以及为什么这样做的注释。让我知道这是否对您或其他任何人有帮助。这是我第一次回复帖子。

PROCEDURE UpsertTSJobData(ActivitySeq_in IN NUMBER,
  Owner_in In VARCHAR2,
  NumTrailers_in IN NUMBER,
  ReleaseFormReceived_in IN NUMBER,
  Response_out OUT VARCHAR2) AS

  err_num NUMBER;
  err_msg VARCHAR2(4000);

  BEGIN
    --This top line essentially does a "SELECT *" from the named table
    --and looks for a match based on the "ON" statement below
    MERGE INTO glob1app.GFS_TS_JOBDATA_TAB tsj
    --This select statement is used for the INSERT when no match
    --is found and the UPDATE when a match is found.
    --It creates a "pseudo-table"
    USING (
      SELECT ActivitySeq_in AS ActSeq,
        Owner_in As Owner,
        NumTrailers_in As NumTrailers,
        ReleaseFormReceived_in As ReleaseFormReceived
        FROM DUAL) input
    --This ON statement is what we're doing the match on to find
    --matching records. This decides whether it will be an
    --INSERT or an UPDATE
    ON (tsj.Activity_seq = ActivitySeq_in)
    WHEN MATCHED THEN
      --Here we UPDATE based on the passed in input table
      UPDATE
        SET OWNER = input.owner,
          NUMTRAILERS = input.NumTrailers,
          RELEASEFORMRECEIVED = input.releaseformreceived
    WHEN NOT MATCHED THEN
      --Here we INSERT based on the passed in input table
      INSERT (
        ACTIVITY_SEQ,
        OWNER,
        NUMTRAILERS,
        RELEASEFORMRECEIVED
        )
      VALUES (
        input.actseq,
        input.owner,
        input.numtrailers,
        input.releaseformreceived
    );

    Response_out := '';

    EXCEPTION
      WHEN OTHERS THEN
        err_num := SQLCODE;
        err_msg := SUBSTR(SQLERRM, 1, 3900);
        Response_out := TO_CHAR (err_num) || ': ' || err_msg;
  END; 
于 2017-04-20T20:59:15.887 回答
0

可能像

DECLARE V_EXISTS NUMBER;
BEGIN SELECT COUNT(*) INTO V_EXISTS FROM TARGET_TABLE WHERE PK_ID = :ID;

    IF V_EXISTS  > 0 THEN
        -- UPDATE
    ELSE
        -- INSERT
    END IF;
END;

此外,您可以尝试使用所谓的临时表(从 DUAL 中选择)

CREATE TABLE TEST (N NUMBER(2), NAME VARCHAR2(20), ADRESS VARCHAR2(100));
INSERT INTO TEST VALUES(1, 'Name1', 'Adress1');
INSERT INTO TEST VALUES(2, 'Name2', 'Adress2');
INSERT INTO TEST VALUES(3, 'Name3', 'Adress3');
SELECT * FROM TEST;
-- test update
MERGE INTO TEST trg
USING (SELECT 1 AS N, 'NameUpdated' AS NAME, 
  'AdressUpdated' AS ADRESS FROM Dual )  src                
ON ( src.N = trg.N )
WHEN MATCHED THEN
    UPDATE 
    SET trg.NAME = src.NAME,
        trg.ADRESS = src.ADRESS
WHEN NOT MATCHED THEN
    INSERT VALUES (src.N, src.NAME, src.ADRESS);
SELECT * FROM TEST;
-- test insert
MERGE INTO TEST trg
USING (SELECT 34 AS N, 'NameInserted' AS NAME, 
  'AdressInserted' AS ADRESS FROM Dual )  src                
ON ( src.N = trg.N )
WHEN MATCHED THEN
    UPDATE 
    SET trg.NAME = src.NAME,
        trg.ADRESS = src.ADRESS
WHEN NOT MATCHED THEN
    INSERT VALUES (src.N, src.NAME, src.ADRESS);
SELECT * FROM TEST;
DROP TABLE TEST;

这里

于 2009-02-28T11:38:17.317 回答
0

很难从您那里确切地说出您的问题是什么,但我认为您希望合并到(或合并到)的表是动态的。在这种情况下,您应该使用DBMS_SQL包来创建动态 SQL

于 2009-02-28T11:40:40.650 回答