0

0

PLSQL开发实现字符串拆分

php中文网

php中文网

发布时间:2016-06-07 17:59:17

|

2156人浏览过

|

来源于php中文网

原创

在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为sz,或sz|nj|zj|nt,在SQL中会这样处理。 select * from tab_1 where col_1=sz ;这是单选框输入。 select * from tab_1 where col_1 =sz|nj ;这是多选框输入。 很明显,多选输入值不

在应用程序开发中,会出现单选或多选框条件输入的需求。如输入框的输入值为'sz',或'sz|nj|zj|nt',在SQL中会这样处理。

select * from tab_1 where col_1='sz' ;这是单选框输入。

select * from tab_1 where col_1 ='sz|nj' ;这是多选框输入。

很明显,多选输入值不会查询出结果。
-


如何解决这个问题?

有使用动态SQL实现的方法,如拼装成这样的SQL语句:select * from tab_1 where col_1 in ('sz','nj') ;

还有将'sz|nj'拆分插入到临时表中,再关联该临时表实现,如select * from tab_1 where col_1 in (select a from tt);

临时表涉及到表的创建和维护,还有IO。

我最近想到一个方法:将传入的字符串以嵌套表类型返回,使用表函数调用,实现这类需求。

函数代码如下:

1.create or replace function f_get_unitstring(p_str_all in varchar2,  
2.                                            p_str_gap in varchar2)   3.  return t_ntb_allstring is  4.  --create or replace type t_ntb_allstring  is table of varchar2(20);   5.  v_ntb_allstring t_ntb_allstring;  
6. 
7.  str_unit varchar2(20);  
8.  str_char varchar2(1);  
9. 
10.  i_str_length number;  
11.  i_str_index  number;  
12. 
13.begin  14.  /*p_str_city:='nj~wx~sz~cz~zj~nt~yc~';*/   15.  --p_str_all := '1|2|3|';   16.  --p_str_gap := '|';   17. 
18.  v_ntb_allstring := t_ntb_allstring();  
19. 
20.  i_str_length := length(p_str_all);  
21. 
22.  i_str_index := 1;  
23. 
24.  while (i_str_index 25.    str_char := substr(p_str_all, i_str_index, 1);  
26.    
27.    if (str_char = p_str_gap) then  28.      
29.      if (str_unit is not null) then  30.        v_ntb_allstring.extend(1);  
31.        v_ntb_allstring(v_ntb_allstring.count) := str_unit;   32.        str_unit := null;   33.      end if;   34.      
35.    else  36.      str_unit := str_unit || str_char;  
37.      
38.      if (i_str_index = i_str_length) then  39.        v_ntb_allstring.extend(1);  
40.        v_ntb_allstring(v_ntb_allstring.count) := str_unit;   41.        str_unit := '';   42.      end if;   43.      
44.    end if;   45.    
46.    i_str_index := i_str_index + 1;  
47.  end loop;   48. 
49.  return(v_ntb_allstring);   50.end;  测试如下:

1.SQL> select * from table(f_get_unitstring('1aa|2cc|3bb','|'));   2.   
3.COLUMN_VALUE  
4.--------------------   5.1aa  
6.2cc  
7.3bb  
8.   
9.SQL>
以上解决方法仅供参数,欢迎交流。

再增加一种方法。

使用pipelined函数也能实现这个需求。但在此处性能优势不会体现出来,如果您碰巧碰到大数据量如亿级别的字符串拆分,该方法就能派上用场。

代码如下:

1.create or replace function f_get_unitstring(p_str_all in varchar2,  
2.                                            p_str_gap in varchar2)   3.  return t_ntb_allstring   4.  pipelined is  5.  --create or replace type t_ntb_allstring  is table of varchar2(20);   6.  v_ntb_allstring t_ntb_allstring;  
7.  str_unit varchar2(20);  
8.  str_char varchar2(1);  
9.  i_str_length number;  
10.  i_str_index  number;  
11.begin  12.  v_ntb_allstring := t_ntb_allstring();  
13.  i_str_length := length(p_str_all);  
14.  i_str_index := 1;  
15.  while (i_str_index 16.    str_char := substr(p_str_all, i_str_index, 1);    
17.    if (str_char = p_str_gap) then  18.      if (str_unit is not null) then  19.        --  v_ntb_allstring.extend(1);   20.        --  v_ntb_allstring(v_ntb_allstring.count) := str_unit;   21.        pipe row(str_unit);  
22.        str_unit := null;   23.      end if;   24.    else  25.      str_unit := str_unit || str_char;  
26.      if (str_unit is not null) then  27.        --  v_ntb_allstring.extend(1);   28.        --  v_ntb_allstring(v_ntb_allstring.count) := str_unit;   29.        pipe row(str_unit);  
30.        str_unit := null;   31.      end if;   32.    end if;   33.    i_str_index := i_str_index + 1;  
34.  end loop;   35.  --return(v_ntb_allstring);   36.end; 

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

129

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

77

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

81

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

60

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

444

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

15

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

12

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

5

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Node.js 教程
Node.js 教程

共57课时 | 7.8万人学习

Rust 教程
Rust 教程

共28课时 | 4万人学习

Vue 教程
Vue 教程

共42课时 | 5.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号