mysql plsql 循环_【原创】PostgreSQL 遍历表的PLSQL列举

我们经常会对数据字典中的系统表进行遍历,从而写一些SHELL脚本或者动态造数据等等。 这里我用PLSQL演示了三种方法来遍历一张表。

表结构如下,t_girl=# \d tmp_1;

Unlogged table "public.tmp_1"

Column | Type | Modifiers

----------+-----------------------------+-----------

id | integer |

log_time | timestamp without time zone |

在这里我创建里一个自定义类型来保存我的函数返回值。create type ytt_record as (id int,log_time timestamp without time zone);

现在来看第一个函数。 也是用最笨的方法来遍历。create or replace function sp_test_record1(

IN f_id int

) returns setof ytt_record as

$ytt$

declare i int;

declare cnt int;

declare o_out ytt_record;

begin

i := 0;

cnt := 0;

select count(*) into cnt from tmp_1 where id > f_id;

while i < cnt

loop

select id,log_time into strict o_out from tmp_1 where id > f_id order by log_time desc limit 1 offset

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值