在PostgreSQL中使用字符串进行循环 [英] For loops with string in PostgreSQL

查看:309
本文介绍了在PostgreSQL中使用字符串进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我尝试使用,连接字符串:

Here I am trying to concatenate a string with ,:

CREATE FUNCTION looping() RETURNS TABLE(round text)
DECLARE
  i RECORD;
BEGIN

FOR i IN select regexp_split_to_table('33,55,66,88', ',') as "asd"
LOOP
  str:= str || ',' ||(select  i."asd");
END LOOP;

END;
$$ LANGUAGE plpgsql; 

是真的还是我错过了什么?

Is it true or am I missing something?

推荐答案

通常,具有标准SQL函数的基于集合的操作优于循环.
但是,如果您需要plpgsql中的控件结构,则可以这样工作(多种方式之一):

Often, a set-based operation with standard SQL functions is superior to looping.
But if you need the control structure in plpgsql, would work like this (one of many ways):

CREATE FUNCTION f_loop2(OUT str text)
  RETURNS text AS
$func$
DECLARE
   i text;
BEGIN
str := '';

FOR i IN
   SELECT regexp_split_to_table('33,55,66,88', ',')
LOOP
  str :=  str || ',' || i;
END LOOP;

END
$func$ LANGUAGE plpgsql; 

这篇关于在PostgreSQL中使用字符串进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆