sql加上upsert的存储过程

时间:2015-04-16 04:26:53

标签: sql oracle sqlplus

我是一个完整的新手,我被要求为upsert编写存储过程

我想做以下事情:

exectute upsert(1,'locationA');

我谷歌并发现:

CREATE OR REPLACE PROCEDURE TABLE_UPSERT (v_id IN NUMBER,
                                          v_searches IN VARCHAR2(20),
                                          v_account IN VARCHAR2(20)) AS
BEGIN
  INSERT INTO table (id, searches, account) VALUES (v_id, v_searches, v_account) ;
EXCEPTION
  WHEN DUP_VAL_ON_INDEX THEN
    UPDATE TABLE
    SET searches = v_searches, account = v_account
    WHERE id = v_id;
END;
/

然后我尝试将其修改为:

    CREATE OR REPLACE PROCEDURE upsert (a IN NUMBER, b IN varchar(40) ) 
    AS
    BEGIN
      INSERT INTO zone_location VALUES (a, b) ;
    EXCEPTION
      WHEN DUP_VAL_ON_INDEX THEN
      UPDATE zone_location
      SET location = b
      WHERE zid = a;
    END;
    /

即使没有EXCEPTION部分,我也被告知: PLS-00103:在期待......时遇到符号"("

感谢您的关注。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

参数类型不需要大小。将VARCHAR2(20)更改为VARCHAR2

SQL> create or replace procedure test_procedure(a varchar2)
  2  is
  3  begin
  4    null;
  5  end;
  6  /

Procedure created.

SQL * Plus错误消息包含有助于指出问题的行号和列号。请注意,列号为36个字符,但不包括create or replace

SQL> create or replace procedure test_procedure(a varchar2(10))
  2  is
  3  begin
  4    null;
  5  end;
  6  /

Warning: Procedure created with compilation errors.

SQL> show errors
Errors for PROCEDURE TEST_PROCEDURE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
1/36     PLS-00103: Encountered the symbol "(" when expecting one of the
         following:
         := . ) , @ % default character
         The symbol ":=" was substituted for "(" to continue.