创建一个存储过程,该存储过程循环传入参数的多个值

时间:2018-04-11 17:56:25

标签: sql oracle plsql

所以我正在使用JAVA程序调用存储过程并将数据从XML文件传递到我的存储过程中设置的参数。我试过从oracle中调用存储过程,它们适用于单个值。即使存在多个值,此存储过程是否仍然有效?我是否需要包含某种FOR循环以确保插入所有值?  以下是我的代码:

CREATE OR REPLACE PROCEDURE pega_submission_value (
    rsubmission_id   IN NUMBER,
    rvalue_tx        IN VARCHAR,
    rutc_offset      IN NUMBER,
    rdata_date       IN VARCHAR,
    rhr_utc          IN VARCHAR,
    rhr              IN TIMESTAMP,
    rhr_num          IN NUMBER,
    rdata_code       IN VARCHAR,
    rdata_type       IN VARCHAR
) IS
    v_value_id   value.value_id%TYPE;
BEGIN
    NULL;
    INSERT INTO value (
        value_id,
        product_id,
        data_source_id,
        unit_cd,
        value_tx,
        utc_offset,
        data_date,
        hr_utc,
        hr,
        hr_num,
        data_code,
        create_dt,
        create_user_id
    ) VALUES (
        NULL,
        555,
        3,
        'NA',
        rvalue_tx,
        rutc_offset,
        rdata_date,
        rhr_utc,
        rhr,
        rhr_num,
        rdata_code,
        SYSDATE,
        '15'
    ) RETURNING value_id INTO v_value_id;

    INSERT INTO submission_value (
        submission_id,
        value_id,
        form_field_id,
        create_dt,
        create_user_id,
        modify_dt,
        modify_user_id,
        effective_dt,
        inactive_dt
    ) VALUES (
        rsubmission_id,
        v_value_id,
        (
            SELECT
                form_field_id
            FROM
                form_field
            WHERE
                form_field_tx = rdata_type
        ),
        SYSDATE,
        '777',
        NULL,
        NULL,
        NULL,
        NULL
    );

    COMMIT;
END pega_submission_value;
/

1 个答案:

答案 0 :(得分:1)

如果要将多个值插入到表中,则应在Java中调用循环中的过程,或者应该通过存储过程将数组作为输入参数发送。

首先为记录创建一个类型:

CREATE OR REPLACE TYPE submission_value_rec AS OBJECT
(
  rsubmission_id NUMBER,
  rvalue_tx      VARCHAR,
  rutc_offset    NUMBER,
  rdata_date     VARCHAR,
  rhr_utc        VARCHAR,
  rhr            TIMESTAMP,
  rhr_num        NUMBER,
  rdata_code     VARCHAR,
  rdata_type     VARCHAR
)
/

然后创建一个类型为表的类型:

CREATE OR REPLACE TYPE submission_value_table FORCE AS TABLE OF submission_value_rec
/

然后更改您的存储过程:

CREATE OR REPLACE PROCEDURE pega_submission_value(submission_values submission_value_table) IS
  v_value_id value.value_id%TYPE;
BEGIN
 FOR i IN 1 .. submission_value_table.count LOOP
INSERT INTO VALUE
  (value_id,
   product_id,
   data_source_id,
   unit_cd,
   value_tx,
   utc_offset,
   data_date,
   hr_utc,
   hr,
   hr_num,
   data_code,
   create_dt,
   create_user_id)
VALUES
  (NULL,
   555,
   3,
   'NA',
   submission_value_table(i).rvalue_tx,
   submission_value_table(i).rutc_offset,
   submission_value_table(i).rdata_date,
   submission_value_table(i).rhr_utc,
   submission_value_table(i).rhr,
   submission_value_table(i).rhr_num,
   submission_value_table(i).rdata_code,
   SYSDATE,
   '15')
RETURNING value_id INTO v_value_id;

INSERT INTO submission_value
  (submission_id,
   value_id,
   form_field_id,
   create_dt,
   create_user_id,
   modify_dt,
   modify_user_id,
   effective_dt,
   inactive_dt)
VALUES
  (submission_value_table(i).rsubmission_id,
   v_value_id,
   (SELECT form_field_id
      FROM form_field
     WHERE form_field_tx = submission_value_table(i).rdata_type),
   SYSDATE,
   '777',
   NULL,
   NULL,
   NULL,
   NULL);

COMMIT;
  END LOOP;
END pega_submission_value;

正如您所看到的,我使用了一个循环来向表中插入值。现在您可以在Java程序中创建一个数组,并且可以使用新的输入参数调用过程。