Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
甲骨文sql:“;获取或插入“;存储过程_Sql_Oracle_Stored Procedures - Fatal编程技术网

甲骨文sql:“;获取或插入“;存储过程

甲骨文sql:“;获取或插入“;存储过程,sql,oracle,stored-procedures,Sql,Oracle,Stored Procedures,我想做一个存储过程,它接收一个输入参数,然后 如果目标表不包含该值,则创建新行,然后返回所创建行的id 如果目标表已包含输入参数,则返回该行的id 目前,我仅在input param为new时插入新行: --exemple of table with a primary id a column with value create table unique_number_table ( id NUMBER(12) not null, UNIQUE_NUMBER

我想做一个存储过程,它接收一个输入参数,然后

  • 如果目标表不包含该值,则创建新行,然后返回所创建行的id
  • 如果目标表已包含输入参数,则返回该行的id
目前,我仅在input param为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;

你知道怎么做吗?

在我看来,你想要的是存储函数而不是过程

创建或替换函数插入或获取唯一编号(输入编号varchar2)
返回唯一的\u编号\u表。ID%类型
是
L_NUM UNIQUE_NUMBER_表。ID%类型;
开始
选择ID
进入L_NUM
从唯一\u编号\u表
其中,唯一\u编号=输入\u编号;
返回L_NUM;
例外
当找不到数据时
插入唯一\u编号\u表(id,唯一\u编号)
值(序号.NEXTVAL,输入序号)
将ID返回到L_NUM中;
返回L_NUM;
结束插入或获取唯一编号;

这是您问题的可能解决方案

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;
您可以像这样测试该过程

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;