简体   繁体   English

oracle sql:“获取或插入”存储过程

[英]oracle sql : "get or insert" stored procedure

I would like to do a stored procedure that receive an input param and then我想做一个接收输入参数的存储过程,然后

  • if targeted table does not contains that value, a new row is created and then the id of the created row is returned如果目标表不包含该值,则创建一个新行,然后返回创建的行的 id
  • if targeted table already contain input param, the id of the row is returned如果目标表已经包含输入参数,则返回该行的 id

For moment I only manage to insert new row only if input param is new:目前我只在输入参数是新的情况下才设法插入新行:

--exemple of table with a primary id a column with value
create table unique_number_table  (
 id             NUMBER(12)   not null,
 UNIQUE_NUMBER VARCHAR2(80)  not null,

 constraint PK_ID primary key (ID)
);

    create sequence SEQ_NUMBER
    INCREMENT BY 1
    START WITH 2
    MAXVALUE 999999999 
    MINVALUE 0; 

   create or replace procedure insert_or_get_unique_number ( input_number in varchar ) is     
    begin 
    insert into unique_number_table (id, UNIQUE_NUMBER) 
    select SEQ_NUMBER.NEXTVAL ,input_number
    from dual
    where not exists(select *  from unique_number_table 
                 where UNIQUE_NUMBER =input_number);   
     end insert_or_get_unique_number;

Do you know how to do this?你知道怎么做吗?

Seems to me like you want a stored function and not a procedure.在我看来,你想要一个存储函数而不是一个过程。

create or replace function insert_or_get_unique_number (input_number varchar2)
   return UNIQUE_NUMBER_TABLE.ID%type
is
  L_NUM  UNIQUE_NUMBER_TABLE.ID%type;
begin
   select ID
     into L_NUM
     from UNIQUE_NUMBER_TABLE
    where UNIQUE_NUMBER = input_number;
   return L_NUM;
exception
   when NO_DATA_FOUND then
      insert into unique_number_table (id, UNIQUE_NUMBER)
      values (SEQ_NUMBER.NEXTVAL, input_number)
      returning ID into L_NUM;
      return L_NUM;
end insert_or_get_unique_number;

This is a possible solution to your problem.这是您问题的可能解决方案。

CREATE OR REPLACE PROCEDURE insert_or_get_unique_number (
                            input_number IN VARCHAR,
                            c_out out sys_refcursor 
) IS

Lv_input_exists INT;
lv_myRowid VARCHAR2(200);
err_code varchar2(600);
err_msg varchar2(500);

BEGIN

  --step 1 check if the input param exists. -
  
  select count(*)
  INTO Lv_input_exists
  FROM unique_number_table
  WHERE unique_number = input_number; 
  
  --step 2 if it exists than get the rowid of that row and return that value
   IF Lv_input_exists >  0
    THEN
     OPEN c_out for 
     SELECT  ROWID
     FROM    unique_number_table Uni
     WHERE  uni.id = input_number ;
     RETURN;
     
    ELSE
  -- STEP 3  the input number does not exists therefore we need to insert and return the rowid--   

    INSERT INTO unique_number_table (
        id,
        unique_number
    )
     VALUES(
            seq_number.NEXTVAL,
            input_number)
          returning ROWID into lv_myRowid;

    ----STEP 4 Open the cursor and return get the rowid.
        OPEN c_out for 
        SELECT  lv_myRowid
        FROM DUAL ;
        
  
    SYS.dbms_output.put_line( 'Done' );
            
    END IF;
    
    EXCEPTION  WHEN OTHERS THEN 
    err_code := SQLCODE;
    err_msg := SUBSTR(SQLERRM, 1, 200);
    
     SYS.dbms_output.put_line( err_code || ' '||': '||err_msg );
    

END insert_or_get_unique_number;

you can test the procedure like so.你可以像这样测试程序。

set serveroutput on ;
DECLARE
  INPUT_NUMBER VARCHAR2(200);
  C_OUT sys_refcursor;
BEGIN
  INPUT_NUMBER := '3';

  INSERT_OR_GET_UNIQUE_NUMBER(
    INPUT_NUMBER => INPUT_NUMBER,
    C_OUT => C_OUT
  );
  
 DBMS_SQL.return_result(C_OUT);
   

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM