oracle 一行变多行,pl/sql
2013-11-23 22:29
776 查看
建表语句:
create table MY_CHANGE (MY_ID int,
MY_VALUE varchar(100));
insert into MY_CHANGE values(1,'a,b,c');
insert into MY_CHANGE values(2,'d,r');
insert into MY_CHANGE values(3,'x,y,z');
commit;
所谓的一行变多行就是要实现下面的效果:
1 a
1 b
1 c
2 d
2 r
3 x
3 y
3 z
pl/sql:
declare num int; --判断表是否存在的标识
cur_my_ID int; --游标变量值
cur_my_value varchar2(100); --游标变量值
s1 varchar(100);
s2 varchar(100); --s1,s2为拆分做准备
len_1 int; --字符串长度
cursor cur_my is
select my_ID,my_value from my_change; --定义游标
begin
/*
* 存在表MY_TABLE则删除,不存在则创建
*/
select count(1) into num from all_tables where TABLE_NAME = 'MY_TABLE' and OWNER='SCOTT';
if(num=1) then
execute immediate 'drop table MY_TABLE';
end if;
execute immediate '
create table MY_TABLE(
my_ID int,
my_value_now varchar2(100))';
open cur_my;
fetch cur_my into cur_my_id,cur_my_value;
while cur_my%found
loop --外循环,游标取值
select replace(cur_my_value,',','') into s1 from dual; -- 去除字符中间的 ,
select length(s1) into len_1 from dual; -- 取当前字符串长度
while len_1>0 --内循环,拆分数据
loop
s2:=substr(s1,1,1); -- 取出第一个字符
insert into my_table values(cur_my_id,s2); --插入
s1:=substr(s1,2,len_1-1); -- 把第一个字符删除
len_1:=len_1-1; -- 循环次数-1
end loop;
fetch cur_my into cur_my_id,cur_my_value;
end loop;
close cur_my;
end ;
存储过程:
create or replace
procedure cc as
num int; --判断表是否存在
cur_my_ID int; --游标值
cur_my_value varchar2(100); --游标值
s1 varchar(100);
s2 varchar(100);
len_1 int;
cursor cur_my is
select my_ID,my_value from my_change;
str_sql varchar2(500);
tb_name varchar2(100);
begin
tb_name:='MY_TABLE';
select count(1) into num from all_tables where TABLE_NAME = 'MY_TABLE' and OWNER='SCOTT';
if(num=1) then
str_sql:='drop table '|| tb_name;
execute immediate str_sql;
end if;
str_sql:=' create table '|| tb_name ||' (
my_ID int,
my_value_now varchar2(100))';
execute immediate str_sql;
open cur_my;
fetch cur_my into cur_my_id,cur_my_value;
while cur_my%found
loop
select replace(cur_my_value,',','') into s1 from dual; -- 去除字符中间的 ,
select length(s1) into len_1 from dual; -- 取当前字符串长度
while len_1>0
loop
s2:=substr(s1,1,1); -- 取出第一个字符
str_sql:='insert into '|| tb_name|| '(my_ID,my_value_now) values(' || cur_my_id ||','''|| s2 ||''')'; --两个单引号输出单引号
execute immediate str_sql;
-- dbms_output.put_line(s2); -- 输出
s1:=substr(s1,2,len_1-1); -- 把第一个字符删除
len_1:=len_1-1; -- 循环次数-1
end loop;
fetch cur_my into cur_my_id,cur_my_value;
end loop;
close cur_my;
end cc;
create table MY_CHANGE (MY_ID int,
MY_VALUE varchar(100));
insert into MY_CHANGE values(1,'a,b,c');
insert into MY_CHANGE values(2,'d,r');
insert into MY_CHANGE values(3,'x,y,z');
commit;
所谓的一行变多行就是要实现下面的效果:
1 a
1 b
1 c
2 d
2 r
3 x
3 y
3 z
pl/sql:
declare num int; --判断表是否存在的标识
cur_my_ID int; --游标变量值
cur_my_value varchar2(100); --游标变量值
s1 varchar(100);
s2 varchar(100); --s1,s2为拆分做准备
len_1 int; --字符串长度
cursor cur_my is
select my_ID,my_value from my_change; --定义游标
begin
/*
* 存在表MY_TABLE则删除,不存在则创建
*/
select count(1) into num from all_tables where TABLE_NAME = 'MY_TABLE' and OWNER='SCOTT';
if(num=1) then
execute immediate 'drop table MY_TABLE';
end if;
execute immediate '
create table MY_TABLE(
my_ID int,
my_value_now varchar2(100))';
open cur_my;
fetch cur_my into cur_my_id,cur_my_value;
while cur_my%found
loop --外循环,游标取值
select replace(cur_my_value,',','') into s1 from dual; -- 去除字符中间的 ,
select length(s1) into len_1 from dual; -- 取当前字符串长度
while len_1>0 --内循环,拆分数据
loop
s2:=substr(s1,1,1); -- 取出第一个字符
insert into my_table values(cur_my_id,s2); --插入
s1:=substr(s1,2,len_1-1); -- 把第一个字符删除
len_1:=len_1-1; -- 循环次数-1
end loop;
fetch cur_my into cur_my_id,cur_my_value;
end loop;
close cur_my;
end ;
存储过程:
create or replace
procedure cc as
num int; --判断表是否存在
cur_my_ID int; --游标值
cur_my_value varchar2(100); --游标值
s1 varchar(100);
s2 varchar(100);
len_1 int;
cursor cur_my is
select my_ID,my_value from my_change;
str_sql varchar2(500);
tb_name varchar2(100);
begin
tb_name:='MY_TABLE';
select count(1) into num from all_tables where TABLE_NAME = 'MY_TABLE' and OWNER='SCOTT';
if(num=1) then
str_sql:='drop table '|| tb_name;
execute immediate str_sql;
end if;
str_sql:=' create table '|| tb_name ||' (
my_ID int,
my_value_now varchar2(100))';
execute immediate str_sql;
open cur_my;
fetch cur_my into cur_my_id,cur_my_value;
while cur_my%found
loop
select replace(cur_my_value,',','') into s1 from dual; -- 去除字符中间的 ,
select length(s1) into len_1 from dual; -- 取当前字符串长度
while len_1>0
loop
s2:=substr(s1,1,1); -- 取出第一个字符
str_sql:='insert into '|| tb_name|| '(my_ID,my_value_now) values(' || cur_my_id ||','''|| s2 ||''')'; --两个单引号输出单引号
execute immediate str_sql;
-- dbms_output.put_line(s2); -- 输出
s1:=substr(s1,2,len_1-1); -- 把第一个字符删除
len_1:=len_1-1; -- 循环次数-1
end loop;
fetch cur_my into cur_my_id,cur_my_value;
end loop;
close cur_my;
end cc;
相关文章推荐
- ORACLE纯SQL实现多行合并一行
- Oracle 联接查询,字表多行字段合并为一行,以逗号分隔的查询SQL
- ORACLE纯SQL实现多行合并一行
- Oracle按时间分组统计的sql与oracle 多行合并一行
- Oracle纯SQL实现多行并一行
- ORACLE纯SQL实现多行合并一行【转】
- ORACLE纯SQL实现多行合并一行
- Oracle:一条SQL实现将多行数据并为一行显示
- ORACLE纯SQL实现多行合并一行
- 关于SQL Server/oracle将一列的多行内容拼接成一行的问题讨论
- ORACLE纯SQL实现多行合并一行
- ORACLE纯SQL实现多行合并一行
- ORACLE纯SQL实现多行合并一行【转】
- ORACLE纯SQL实现多行合并一行
- Oracle PL/SQL 语言(Procedural Language/SQL)
- Oracle PL/SQL 可以不安装Oralce客户端
- Windows 7 64位PL/SQL Developer 连接Oracle 11g
- Oracle- PL/SQL DEV工具的使用收集
- ORACLE学习之PL/SQL中异常
- oracle pl/sql 中创建同义词