插入步骤-间隔中的值

时间:2019-04-19 08:17:01

标签: oracle plsql

我需要以下帮助:

我有下表:

CREATE TABLE LOG_PUSH_READOUTS_HEADERS

(

ID NUMBER NOT NULL,

PUSH_DATE DATE NOT NULL,

SOURCE_SERIAL VARCHAR2(100) NOT NULL,

SOURCE_START_DATE DATE NOT NULL,

SOURCE_END_DATE DATE NOT NULL,

SOURCE_RUS_TYPE_ID NUMBER,

OUTPUT_SERIAL VARCHAR2(100) NOT NULL,

FILTERS_RUS VARCHAR2(100),

FILTERS_INDICATORS VARCHAR2(100),

CONSTRAINT id_pk PRIMARY KEY (ID)

);

我需要执行以下过程:

用于插入表中的输入参数是除ID(我们需要为其创建序列器)之外的所有列。输出参数是ID和RESULT_CODE。 ID是插入的主键的值。如果过程成功,则RESULT_CODE为零,或者为9000-9999之间的某个值。将RESULT_CODE用于错误,例如RESULT_CODE 9123为“ FILTER_RUS不能为空”。

这是我的尝试:

CREATE OR REPLACE PROCEDURE INSERT_HEADER
(PUSH_DATE IN DATE, SOURCE_SERIAL IN VARCHAR2, SOURCE_START_DATE IN DATE, SOURCE_END_DATE IN DATE, SOURCE_RUS_TYPE_ID IN NUMBER, 
OUTPUT_SERIAL IN VARCHAR2, FILTERS_RUS IN VARCHAR2, FILTERS_INDICATORS IN VARCHAR2, ID OUT NUMBER, RESULT_CODE OUT NUMBER)

IS
hd_seq NUMBER;

BEGIN

SELECT AMM_MDM.Header_Seq.NEXTVAL INTO hd_seq FROM DUAL;
ID:=hd_seq;
INSERT INTO AMM_MDM.LOG_PUSH_READOUTS_HEADERS (PUSH_DATE IN DATE, SOURCE_SERIAL IN VARCHAR2, SOURCE_START_DATE IN DATE, 
SOURCE_END_DATE IN DATE, SOURCE_RUS_TYPE_ID IN NUMBER, 
OUTPUT_SERIAL IN VARCHAR2, FILTERS_RUS IN VARCHAR2, FILTERS_INDICATORS IN VARCHAR2)
VALUES (PUSH_DATE, SOURCE_SERIAL, SOURCE_START_DATE, 
SOURCE_END_DATE, SOURCE_RUS_TYPE_ID, 
OUTPUT_SERIAL, FILTERS_RUS, FILTERS_INDICATORS)

END;

如何在9000-9999之间设置RESULT_CODE?如何管理错误?

2 个答案:

答案 0 :(得分:0)

我理解问题的方式就像下面的示例。我缩短了列列表(感觉不那么费劲),但是您应该能够了解一般的想法。

create or replace procedure p_insert_header
  (p_push_date     in date,
   p_source_serial in varchar2,
   p_filter_rus    in varchar2,
   --
   p_id           out number,
   p_result_code  out number
  )
is
  l_id number;
begin
  -- check for errors
  if p_push_date is null then 
     p_result_code := 1234;
  elsif p_source_serial not in ('A', 'B', 'C') then
     p_result_code := 5532;
  elsif p_filter_rus is null then
     p_result_code := 9123;
  end if;

  -- if there are no errors, do insert
  if p_result_code is null then
     -- fetch sequence number
     p_id := header_seq.nextval;

     insert into log_push_readouts_headers
       (id, push_date, source_serial, filter_rus)
       values
       (p_id, p_push_date, p_source_serial, p_filter_rus);
  end if;
end;

最后,过程完成后,它将返回-作为OUT参数:

  • ID:
    • 一些序号,如果一切正常
    • NULL(如果不是)
  • RESULT_CODE:
    • NULL,如果一切正常
    • 错误编号(如果不是)

答案 1 :(得分:0)

对于用户定义的错误,Oracle允许使用-20000 .. -20999范围内的负整数。您需要删除插入语句的列列表的数据类型声明

CREATE OR REPLACE PROCEDURE INSERT_HEADER(PUSH_DATE          IN DATE,
                                          SOURCE_SERIAL      IN VARCHAR2,
                                          SOURCE_START_DATE  IN DATE,
                                          SOURCE_END_DATE    IN DATE,
                                          SOURCE_RUS_TYPE_ID IN NUMBER,
                                          OUTPUT_SERIAL      IN VARCHAR2,
                                          FILTERS_RUS        IN VARCHAR2,
                                          FILTERS_INDICATORS IN VARCHAR2,
                                          ID                 OUT NUMBER,
                                          RESULT_CODE        OUT NUMBER)
IS
BEGIN
  INSERT INTO LOG_PUSH_READOUTS_HEADERS
    (ID,
     PUSH_DATE,
     SOURCE_SERIAL,
     SOURCE_START_DATE,
     SOURCE_END_DATE,
     SOURCE_RUS_TYPE_ID,
     OUTPUT_SERIAL,
     FILTERS_RUS,
     FILTERS_INDICATORS)
  VALUES
    (Header_Seq.NEXTVAL,
     PUSH_DATE,
     SOURCE_SERIAL,
     SOURCE_START_DATE,
     SOURCE_END_DATE,
     SOURCE_RUS_TYPE_ID,
     OUTPUT_SERIAL,
     FILTERS_RUS,
     FILTERS_INDICATORS);

  --Suppose some opertions performed and gave the RESULT_CODE value below
     RESULT_CODE := 20123;

  IF    RESULT_CODE = 20123 THEN raise_application_error(-RESULT_CODE, 'FILTER_RUS cannot be null'); 
--ELSIF RESULT_CODE = 20124 THEN raise_application_error(-RESULT_CODE, 'Some other message 1');
--ELSIF RESULT_CODE = 20125 THEN raise_application_error(-RESULT_CODE, 'Some other message 2');
  END IF;     
END;

在这种情况下,它会显示以下消息:FILTER_RUS cannot be null