oracle中猜分字符串成多个字段

时间:2022-10-16 14:59:14

 有一个数据表,其中的一个字段比较长,是由多个属性组成,例如:

oracle中猜分字符串成多个字段SQL >   select  code_string,项目名称,code_value  from  code_standard;
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段CODE_STRING          项目名称                       CODE_VALUE
oracle中猜分字符串成多个字段
-- ------------------ ------------------------------ --------------------------------------------------------------------------------
oracle中猜分字符串成多个字段
210403006011          螺母                           物料大分类码段,标准件,203|标准件分类码段,紧固件,204|紧固件分类码段,螺母,215|标准代号码段,GB/T6178-1986,530|规格码段,M20,1371|材料(强度等级)码段,8,1718|表面处理码段,氧化,506 oracle中猜分字符串成多个字段

第三个字段非常的长,希望猜分成 这样:

210403006011 螺母 标准件 紧固件 螺母 GB/T6178-1986 M20 8 氧化 oracle中猜分字符串成多个字段

1,创建一个 split 函数

oracle中猜分字符串成多个字段create   or   replace  type type_split  as   table   of   varchar2 ( 50 );   -- 创建一个  type  ,如果为了使split函数具有通用性,请将其size 设大些。
oracle中猜分字符串成多个字段

oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
-- 创建function
oracle中猜分字符串成多个字段
create   or   replace   function  split
oracle中猜分字符串成多个字段(
oracle中猜分字符串成多个字段   p_list 
varchar2 ,
oracle中猜分字符串成多个字段   p_sep 
varchar2  : =   ' , '
oracle中猜分字符串成多个字段)  
return  type_split pipelined
oracle中猜分字符串成多个字段 
is
oracle中猜分字符串成多个字段   l_idx  pls_integer;
oracle中猜分字符串成多个字段   v_list  
varchar2 ( 50 ) : =  p_list;
oracle中猜分字符串成多个字段
begin
oracle中猜分字符串成多个字段   loop
oracle中猜分字符串成多个字段      l_idx :
=  instr(v_list,p_sep);
oracle中猜分字符串成多个字段      
if  l_idx  >   0   then
oracle中猜分字符串成多个字段          
pipe  row(substr(v_list, 1 ,l_idx - 1 ));
oracle中猜分字符串成多个字段          v_list :
=  substr(v_list,l_idx + length(p_sep));
oracle中猜分字符串成多个字段      
else
oracle中猜分字符串成多个字段          
pipe  row(v_list);
oracle中猜分字符串成多个字段          
exit ;
oracle中猜分字符串成多个字段      
end   if ;
oracle中猜分字符串成多个字段   
end  loop;
oracle中猜分字符串成多个字段   
return ;
oracle中猜分字符串成多个字段
end  split;

2,创建主函数

oracle中猜分字符串成多个字段-- --创建类型
oracle中猜分字符串成多个字段
create   or   replace  type m_type  is  object(a  varchar2 ( 50 ),b  varchar2 ( 50 ),c  varchar2 ( 50 ),d  varchar2 ( 50 ),e  varchar2 ( 50 ),f  varchar2 ( 50 ),g  varchar2 ( 50 ),h  varchar2 ( 50 ),k varchar2(50))
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
create   or   replace  type m_table  is   table    of  m_type
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
-- ---创建function
oracle中猜分字符串成多个字段
create   or   replace   function  getCodeData  return  m_table
oracle中猜分字符串成多个字段    
as
oracle中猜分字符串成多个字段     
cursor  ca  is   select  code_string  as  item_code,项目名称  as  item_name,code_value  from  code_standard;
oracle中猜分字符串成多个字段     rs ca
% rowtype;   
oracle中猜分字符串成多个字段     rs2 m_table:
= m_table();   
oracle中猜分字符串成多个字段     
oracle中猜分字符串成多个字段     type cursor_type 
is  ref  cursor ;
oracle中猜分字符串成多个字段     type record_type 
is  record(
oracle中猜分字符串成多个字段        a 
varchar2 ( 50 )       
oracle中猜分字符串成多个字段     );
oracle中猜分字符串成多个字段     
oracle中猜分字符串成多个字段     c1 cursor_type;
oracle中猜分字符串成多个字段     r_c1 record_type;
oracle中猜分字符串成多个字段     
oracle中猜分字符串成多个字段     c2 cursor_type;
oracle中猜分字符串成多个字段     r_c2 record_type;
oracle中猜分字符串成多个字段      
oracle中猜分字符串成多个字段     TYPE  type_arrry 
IS   TABLE   OF   VARCHAR2 ( 50 );
oracle中猜分字符串成多个字段     myArray type_arrry:
= type_arrry();
oracle中猜分字符串成多个字段      
oracle中猜分字符串成多个字段     mysql 
varchar2 ( 500 );
oracle中猜分字符串成多个字段     
oracle中猜分字符串成多个字段     i 
integer : = 0 ;
oracle中猜分字符串成多个字段     m 
integer ;
oracle中猜分字符串成多个字段     n 
integer ;
oracle中猜分字符串成多个字段 
oracle中猜分字符串成多个字段     
begin
oracle中猜分字符串成多个字段     
oracle中猜分字符串成多个字段         n:
= 1 ;
oracle中猜分字符串成多个字段         m:
= 1 ;
oracle中猜分字符串成多个字段         myArray.extend(
10 );
oracle中猜分字符串成多个字段         
open  ca;
oracle中猜分字符串成多个字段         loop
oracle中猜分字符串成多个字段            
fetch  ca  into  rs;
oracle中猜分字符串成多个字段            
exit   when  ca % notfound;
oracle中猜分字符串成多个字段            
oracle中猜分字符串成多个字段            rs2.extend(
1 );
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段            myArray(
1 ): = rs.item_code;
oracle中猜分字符串成多个字段            myArray(
2 ): = rs.item_name;
oracle中猜分字符串成多个字段            i:
= 3 ;
oracle中猜分字符串成多个字段            mysql:
= ' select * from table(split( ''' ||  rs.code_value  || ''' , '' | '' )) ' ;
oracle中猜分字符串成多个字段            
open  c1  for  mysql;
oracle中猜分字符串成多个字段            loop
oracle中猜分字符串成多个字段              
fetch  c1  into  r_c1;
oracle中猜分字符串成多个字段              
exit   when  c1 % notfound;      
oracle中猜分字符串成多个字段              mysql:
= ' select * from table(split( '''   ||  r_c1.a  ||   ''' , '' , '' )) ' ;
oracle中猜分字符串成多个字段              n:
= 1 ;
oracle中猜分字符串成多个字段              
open  c2  for  mysql;
oracle中猜分字符串成多个字段              loop
oracle中猜分字符串成多个字段                 
fetch  c2  into  r_c2;
oracle中猜分字符串成多个字段                 
exit   when  c2 % notfound;
oracle中猜分字符串成多个字段                   
if  n = 2   then
oracle中猜分字符串成多个字段                       
-- myArray.extend(1);
oracle中猜分字符串成多个字段
                       myArray(i): = r_c2.a;
oracle中猜分字符串成多个字段                   
end   if ;
oracle中猜分字符串成多个字段                   n:
= n + 1 ;
oracle中猜分字符串成多个字段              
end  loop; 
oracle中猜分字符串成多个字段              
close  c2;   
oracle中猜分字符串成多个字段              i:
= i + 1 ;    
oracle中猜分字符串成多个字段            
end  loop;
oracle中猜分字符串成多个字段            
close  c1;  
oracle中猜分字符串成多个字段            rs2(m):
= m_type(myArray( 1 ),myArray( 2 ),myArray( 3 ),myArray( 4 ),myArray( 5 ),myArray( 6 ),myArray( 7 ),myArray( 8 ),myArray(9));
oracle中猜分字符串成多个字段            m:
= m + 1 ;         
oracle中猜分字符串成多个字段         
end  loop;
oracle中猜分字符串成多个字段         
close  ca;
oracle中猜分字符串成多个字段         
oracle中猜分字符串成多个字段         
return  rs2;
oracle中猜分字符串成多个字段   
end ;
oracle中猜分字符串成多个字段

3,测试

oracle中猜分字符串成多个字段SQL >   select   *   from   table (getcodedata)  where  rownum < 10 ;
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段A                                                  B                                                  C                                                  D                                                  E                                                  F                                                  G                                                  H                                                  K
oracle中猜分字符串成多个字段
-- ------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
oracle中猜分字符串成多个字段
210403006011                                        螺母                                               标准件                                             紧固件                                             螺母                                               GB / T6178 - 1986                                       M20                                                 8                                                   氧化
oracle中猜分字符串成多个字段
210401001022                                        螺母                                               标准件                                             紧固件                                             螺母                                               GB / T6181 - 1986                                       M8                                                  04                                                  不经处理
oracle中猜分字符串成多个字段
210504004012                                        垫圈                                               标准件                                             紧固件                                             垫圈                                               GB / T853 - 1988                                         8                                                   Q215                                               不经处理
oracle中猜分字符串成多个字段
210113026011                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T37 - 1988                                         M10 * 40                                               8.8                                                 氧化
oracle中猜分字符串成多个字段
210113002013                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T37 - 1988                                         M12 * 45                                               8.8                                                 镀铬
oracle中猜分字符串成多个字段
210113017011                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T37 - 1988                                         M6 * 30                                                8.8                                                 氧化
oracle中猜分字符串成多个字段
210113005011                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T37 - 1988                                         M8 * 65                                                8.8                                                 氧化
oracle中猜分字符串成多个字段
210111009012                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T5780 - 2000                                       M20 * 110                                              4.8                                                 不经处理
oracle中猜分字符串成多个字段
210107053012                                        螺栓                                               标准件                                             紧固件                                             螺栓                                               GB / T5781 - 2000                                       M10 * 25                                               4.8                                                 不经处理
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段
9  rows selected
oracle中猜分字符串成多个字段
oracle中猜分字符串成多个字段SQL
>