разбить разделенную '$' строку и вставить в таблицу - PullRequest
0 голосов
/ 05 ноября 2019

У меня есть таблица employee с полем emp_id, emp_first_name, emp_last_name, emp_salary.

Я хочу создать процедуру с записями вставки в таблице сотрудника, но в параметре IN процедуры в '1 $ shubham $ tathe $ 5000 #2 $ vijaj $ bakse & 10000 # 3 $ ram $ sharma $ 200 ', и я хочу вставить эту строку в таблицу сотрудников со строкой, разделенной' # ', и полем столбца, разделенным' $ '.

emp_id   |  emp_first_name |  emp_last_name  |  emp_salary

1           shubham           tathe             5000
2           vijaj             bakse             10000
3           ram               sharma            200

create or replace procedure procedure_split
(
 In_string IN varchar
)

IS

Begin
...
...
...
END;

In_string ='1 $ shubham $ tathe $ 5000 # 2 $ vijaj $ bakse & 10000 # 3 $ ram $ sharma $ 200'

In_string является входным параметром в процедуре.

1 Ответ

2 голосов
/ 05 ноября 2019

Обычно для этого вам не нужен PL / SQL.

Таблица:

SQL> create table test
  2    (emp_id number,
  3     emp_first_name varchar2(20),
  4     emp_last_name  varchar2(20),
  5     emp_salary number);

Table created.

Код:

SQL> insert into test (emp_id, emp_first_name, emp_last_name, emp_salary)
  2  with
  3  data (col) as
  4    (select '1$shubham$tathe$5000#2$vijaj$bakse$10000#3$ram$sharma$200' from dual),
  5  red as
  6    (select regexp_substr(col, '[^#]+', 1, level) val,
  7            level lvl
  8     from data
  9     connect by level <= regexp_count(col, '#') + 1
 10    ),
 11  emp as
 12    (select regexp_substr(val, '\w+', 1, 1) emp_id,
 13            regexp_substr(val, '\w+', 1, 2) emp_first_name,
 14            regexp_substr(val, '\w+', 1, 3) emp_last_name,
 15            regexp_substr(val, '\w+', 1, 4) emp_salary
 16     from red
 17    )
 18  select * From emp;

3 rows created.

Результат:

SQL> select * From test;

    EMP_ID EMP_FIRST_NAME       EMP_LAST_NAME        EMP_SALARY
---------- -------------------- -------------------- ----------
         1 shubham              tathe                      5000
         2 vijaj                bakse                     10000
         3 ram                  sharma                      200

SQL>

Если должно быть процедурой, проблем тоже нет.

Процедура:

SQL> rollback;

Rollback complete.

SQL> create or replace procedure p_test (par_col in varchar2) is
  2  begin
  3    insert into test (emp_id, emp_first_name, emp_last_name, emp_salary)
  4    with
  5    red as
  6      (select regexp_substr(par_col, '[^#]+', 1, level) val,
  7              level lvl
  8       from dual
  9       connect by level <= regexp_count(par_col, '#') + 1
 10      ),
 11    emp as
 12      (select regexp_substr(val, '\w+', 1, 1) emp_id,
 13              regexp_substr(val, '\w+', 1, 2) emp_first_name,
 14              regexp_substr(val, '\w+', 1, 3) emp_last_name,
 15              regexp_substr(val, '\w+', 1, 4) emp_salary
 16       from red
 17      )
 18    select * From emp;
 19  end;
 20  /

Procedure created.

Проверка:

SQL> exec p_test('1$shubham$tathe$5000#2$vijaj$bakse$10000#3$ram$sharma$200');

PL/SQL procedure successfully completed.

SQL> select * From test;

    EMP_ID EMP_FIRST_NAME       EMP_LAST_NAME        EMP_SALARY
---------- -------------------- -------------------- ----------
         1 shubham              tathe                      5000
         2 vijaj                bakse                     10000
         3 ram                  sharma                      200

SQL>
...