拆分 '$' 分隔的字符串并插入 table
split '$' seperated string and insert into table
我有 table 名员工 emp_id,emp_first_name,emp_last_name,emp_salary
场.
我想创建一个在员工中插入记录的过程 table 但是在过程 IN 参数中 '1$shubham$tathe$5000#2$vijaj$bakse&10000#3$ram$sharma$200' 和我想在员工 table 中插入此字符串,行由“#”分隔,列字段由“$”分隔。
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是程序中的输入参数。
基本上,您不需要 PL/SQL。
Table:
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$tathe00#2$vijaj$bakse000#3$ram$sharma0' 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$tathe00#2$vijaj$bakse000#3$ram$sharma0');
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>
我有 table 名员工 emp_id,emp_first_name,emp_last_name,emp_salary 场.
我想创建一个在员工中插入记录的过程 table 但是在过程 IN 参数中 '1$shubham$tathe$5000#2$vijaj$bakse&10000#3$ram$sharma$200' 和我想在员工 table 中插入此字符串,行由“#”分隔,列字段由“$”分隔。
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是程序中的输入参数。
基本上,您不需要 PL/SQL。
Table:
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$tathe00#2$vijaj$bakse000#3$ram$sharma0' 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$tathe00#2$vijaj$bakse000#3$ram$sharma0');
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>