将spam_keyword表word字段的字符全部拆分,只是利用过程语言完成循环的操作而已。

  1. create or replace function proc1() returns setof text as $$
  2. declare
  3. str text;
  4. strlength int;
  5. strreturn text;
  6. i int;
  7. curs1 refcursor; --声明游标
  8. begin
  9. open curs1 for select replace(word,' ','') from spam_keyword; --在loop前打开游标并绑定好范围,query定义范围的时候就把空格都替换掉
  10. <<loop1>> --标签
  11. loop
  12. fetch curs1 into str;
  13. if not found then exit;
  14. end if;
  15. i:=0; --每一次游标完成赋值后就把i初始化为0
  16. <<loop2>>
  17. loop
  18. i:=i+1;
  19. strreturn:=substring(str,i,1);
  20. strlength:=char_length(str);
  21. return next strreturn;
  22. exit when i>=strlength;
  23. end loop loop2;
  24. end loop loop1;
  25. close curs1;
  26. end;
  27. $$ language plpgsql

返回的是setof结果集,再用分组查询可以统计到各个字符的数量。

  1. select proc1,count(*) from proc1() group by proc1 order by count desc;
  1. proc1 | count
  2. -------+-------
  3. | 65
  4. | 61
  5. | 41
  6. 1 | 38
  7. | 36
  8. 0 | 35
  9. 5 | 32
  10. ...省略1000行左右
  11. | 1
  12. | 1
  13. | 1
  14. | 1
  15. | 1
  16. } | 1
  17. (1145 行记录)

写成通用的函数,通过指定参数来分割

  1. create or replace function split_to_table(str text) returns setof text as $$
  2. declare
  3. strlength int;
  4. strreturn text;
  5. i int;
  6. begin
  7. i:=0;
  8. loop
  9. i:=i+1;
  10. str:=replace(str,' ','');
  11. strlength:=char_length(str);
  12. strreturn:=substring(str,i,1);
  13. return next strreturn;
  14. exit when i>=strlength;
  15. end loop;
  16. end;
  17. $$ language plpgsql

这样使用

  1. 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)

  1. select regexp_split_to_table(word,E'') from spam_keyword;

PostgreSQL-PL/pgSQL-cursor,loop的更多相关文章

  1. postgresql PL/pgSQL—存储过程结构和变量声明

    ref: https://www.postgresql.org/docs/9.6/static/plpgsql-structure.html 一. 函数结构 CREATE FUNCTION somef ...

  2. 用PL/pgSQL写postgreSQL的存储过程[转]

    http://blog.chinaunix.net/uid-7591044-id-1742967.html 今天学会了用 PL/pgSQL 写 postgreSQL 的存储过程,网上资料实在少得可怜, ...

  3. PostgreSQL存储过程(2)-基于PL/PgSQL的存储过程

    介绍 PL/pgSQL 是PostgreSQL 数据库系统的一个可加载的过程语言. PL/pgSQL 的设计目标是创建一种可加载的过程语言,可以 用于创建函数和触发器过程, 为SQL 语言增加控制结构 ...

  4. postgreSQL PL/SQL编程学习笔记(五)——触发器(Triggers)

    Trigger Procedures PL/pgSQL can be used to define trigger procedures on data changes or database eve ...

  5. postgreSQL PL/SQL编程学习笔记(三)——游标(Cursors)

    Cursors Rather than executing a whole query at once, it is possible to set up a cursor that encapsul ...

  6. postgreSQL PL/SQL编程学习笔记(二)

    Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...

  7. postgreSQL PL/SQL编程学习笔记(一)

    1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...

  8. PL/pgSQL学习笔记之八

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 另外一种声明 PL/pgSQL 函数的方法是使用 returns ...

  9. PL/pgSQL学习笔记之七

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名 ...

  10. PL/pgSQL学习笔记之五

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3. 声明 块中使用的所有的变量必须在块的声明节中进行声明 ...

随机推荐

  1. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  2. php中使用fsockopen实现异步请求

    php执行一段程序,有可能几毫秒就执行完毕,也有可能耗时较长.例如,用户下单这个事件,如果调用了些第三方服务进行发邮件.短信.推送等通知,可能导致前端一直在等待.而有的时候,我们并不关心这些耗时脚本的 ...

  3. ActiveMQ5.14.1+Zookeeper3.4.9高可用伪分布式部署

    本文借鉴http://www.cnblogs.com/gossip/p/5977489.html,在此基础上进行了完善,使之成为一个完整版的伪分布式部署说明,在此记录一下! 一.本文目的       ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...

  5. C#字符串的倒序输出

    介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...

  6. 网站文件系统发展&&分布式文件系统fastDFS

    网站文件系统发展 1.单机时代的图片服务器架构 初创时期由于时间紧迫,开发人员水平也很有限等原因.所以通常就直接在website文件所在的目录下,建立1个upload子目录,用于保存用户上传的图片文件 ...

  7. jQuery中取消后续执行的内容

    <html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title&g ...

  8. C# webform上传图片并生成缩略图

    其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...

  9. 在DevExpress程序中使用Winform分页控件直接录入数据并保存

    一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...

  10. WebSocket异常 通常每个套接字地址(协议/网络地址/端口)只允许使用一次

    websocket的实例:http://blog.csdn.net/for_cxc/article/details/51500185 问题: 新建一个连接通信没有问题,但是如果关闭再建立就会报错:通常 ...