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. 声明 块中使用的所有的变量必须在块的声明节中进行声明 ...
随机推荐
- 网站使用https协议
了解https HTTPS 是以安全为目标的 HTTP 通道,即 HTTP 下加入 SSL 加密层.HTTPS 不同于 HTTP 的端口,HTTP默认端口为80,HTTPS默认端口为443. SSL ...
- Atitit. 提升存储过程与编程语言的可读性解决方案v3 qc25.docx
Atitit. 提升存储过程与编程语言的可读性解决方案v3 qc25.docx 1. 大原则:分解+命名1 1.1. 命名规范1 1.2. 分层.DI和AOP是继OO1 1.3. 运算符可读性一般要比 ...
- SQL:插入指定标识列的数据时候的小错误
异常处理汇总-数据库系列 http://www.cnblogs.com/dunitian/p/4522990.html 后期会在博客首发更新:http://dnt.dkill.net 好久没写标识系 ...
- jQuery LigerUI系列:ligerComboBox
1. ligerComboBox参数.方法及事件 1.1 参数 2. ligerComboBox示例 2.1 初始化HTML select控件 <link href="/Scripts ...
- .Net使用RabbitMQ详解
序言 这里原来有一句话,触犯啦天条,被阉割!!!! 首先不去讨论我的日志组件怎么样.因为有些日志需要走网络,有的又不需要走网路,也是有性能与业务场景的多般变化在其中,就把他抛开,我们只谈消息Rabbi ...
- Android okHttp网络请求之Json解析
前言: 前面两篇文章介绍了基于okHttp的post.get请求,以及文件的上传下载,今天主要介绍一下如何和Json解析一起使用?如何才能提高开发效率? okHttp相关文章地址: Android o ...
- Android数据存储之SQLCipher数据库加密
前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...
- 日常css技巧小结(1)--背景透明度改变对内容无影响
刚开始出现的错误,内容会受到背景透明度改变的影响:如图: 代码: <!DOCTYPE html> <html lang="en"> <head> ...
- 虚拟IP(VIP)
高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA系统是目前企业防止核心计算机系统因故 ...
- 解决MyEclipe出现An error has occurred,See error log for more details的错误
今晚在卸载MyEclipse时出现An error has occurred,See error log for more details的错误,打开相应路径下的文件查看得如下: !SESSION 2 ...