PostgreSQL-PL/pgSQL-cursor,loop
将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已。
- create or replace function proc1() returns setof text as $$
- declare
- str text;
- strlength int;
- strreturn text;
- i int;
- curs1 refcursor; --声明游标
- begin
- open curs1 for select replace(word,' ','') from spam_keyword; --在loop前打开游标并绑定好范围,query定义范围的时候就把空格都替换掉
- <<loop1>> --标签
- loop
- fetch curs1 into str;
- if not found then exit;
- end if;
- i:=0; --每一次游标完成赋值后就把i初始化为0
- <<loop2>>
- loop
- i:=i+1;
- strreturn:=substring(str,i,1);
- strlength:=char_length(str);
- return next strreturn;
- exit when i>=strlength;
- end loop loop2;
- end loop loop1;
- close curs1;
- end;
- $$ language plpgsql
返回的是setof结果集,再用分组查询可以统计到各个字符的数量。
- select proc1,count(*) from proc1() group by proc1 order by count desc;
- proc1 | count
- -------+-------
- 医 | 65
- 院 | 61
- 小 | 41
- 1 | 38
- 州 | 36
- 0 | 35
- 5 | 32
- ...省略1000行左右
- 他 | 1
- 丹 | 1
- 扑 | 1
- 朵 | 1
- 债 | 1
- } | 1
- (1145 行记录)
写成通用的函数,通过指定参数来分割
- create or replace function split_to_table(str text) returns setof text as $$
- declare
- strlength int;
- strreturn text;
- i int;
- begin
- i:=0;
- loop
- i:=i+1;
- str:=replace(str,' ','');
- strlength:=char_length(str);
- strreturn:=substring(str,i,1);
- return next strreturn;
- exit when i>=strlength;
- end loop;
- end;
- $$ language plpgsql
这样使用
- select split_to_table(word) from spam_keyword;
后来突然想起来去看看是不是本来就有分割字符的函数在。。一查果然有。。。席巴。。。
函数:regexp_split_to_array(string text, pattern text [, flags text ])
说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正则表达式将字符串分割成数组
例子:regexp_split_to_array('hello world', E'\\s+') = {hello,world}
函数:regexp_split_to_table(string text, pattern text [, flags text])
说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正则表达式将字符串分割成表格
例子:regexp_split_to_table('hello world', E'\\s+') =
hello
world
(2 rows)
- select regexp_split_to_table(word,E'') from spam_keyword;
PostgreSQL-PL/pgSQL-cursor,loop的更多相关文章
- postgresql PL/pgSQL—存储过程结构和变量声明
ref: https://www.postgresql.org/docs/9.6/static/plpgsql-structure.html 一. 函数结构 CREATE FUNCTION somef ...
- 用PL/pgSQL写postgreSQL的存储过程[转]
http://blog.chinaunix.net/uid-7591044-id-1742967.html 今天学会了用 PL/pgSQL 写 postgreSQL 的存储过程,网上资料实在少得可怜, ...
- PostgreSQL存储过程(2)-基于PL/PgSQL的存储过程
介绍 PL/pgSQL 是PostgreSQL 数据库系统的一个可加载的过程语言. PL/pgSQL 的设计目标是创建一种可加载的过程语言,可以 用于创建函数和触发器过程, 为SQL 语言增加控制结构 ...
- postgreSQL PL/SQL编程学习笔记(五)——触发器(Triggers)
Trigger Procedures PL/pgSQL can be used to define trigger procedures on data changes or database eve ...
- postgreSQL PL/SQL编程学习笔记(三)——游标(Cursors)
Cursors Rather than executing a whole query at once, it is possible to set up a cursor that encapsul ...
- postgreSQL PL/SQL编程学习笔记(二)
Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...
- postgreSQL PL/SQL编程学习笔记(一)
1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...
- PL/pgSQL学习笔记之八
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 另外一种声明 PL/pgSQL 函数的方法是使用 returns ...
- PL/pgSQL学习笔记之七
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名 ...
- PL/pgSQL学习笔记之五
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3. 声明 块中使用的所有的变量必须在块的声明节中进行声明 ...
随机推荐
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- php中使用fsockopen实现异步请求
php执行一段程序,有可能几毫秒就执行完毕,也有可能耗时较长.例如,用户下单这个事件,如果调用了些第三方服务进行发邮件.短信.推送等通知,可能导致前端一直在等待.而有的时候,我们并不关心这些耗时脚本的 ...
- ActiveMQ5.14.1+Zookeeper3.4.9高可用伪分布式部署
本文借鉴http://www.cnblogs.com/gossip/p/5977489.html,在此基础上进行了完善,使之成为一个完整版的伪分布式部署说明,在此记录一下! 一.本文目的 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入
系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...
- C#字符串的倒序输出
介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...
- 网站文件系统发展&&分布式文件系统fastDFS
网站文件系统发展 1.单机时代的图片服务器架构 初创时期由于时间紧迫,开发人员水平也很有限等原因.所以通常就直接在website文件所在的目录下,建立1个upload子目录,用于保存用户上传的图片文件 ...
- jQuery中取消后续执行的内容
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title&g ...
- C# webform上传图片并生成缩略图
其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...
- 在DevExpress程序中使用Winform分页控件直接录入数据并保存
一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...
- WebSocket异常 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
websocket的实例:http://blog.csdn.net/for_cxc/article/details/51500185 问题: 新建一个连接通信没有问题,但是如果关闭再建立就会报错:通常 ...