--数组:
SELECT (ARRAY['{101, 111, 121}', '{201, 211, 221}'])[1]::text[];
SELECT (ARRAY['{101, 111, 121}', '{201, 211, 221}'])::text[];
SELECT (ARRAY['{101, 111, 121}', '{201, 211, 221}'])[1]::text[];
SELECT (ARRAY['{101, 111, 121}'::int[], '{201, 211, 221}'])::int[];
SELECT (ARRAY['{101, 111, 121}'::int[], '{201, 211, 221}'])[1][1];
SELECT (ARRAY['{{101, 111, 121},{201, 211, 221}}'])[1];
SELECT (ARRAY['{{101, 111, 121},{201, 211, 221}}'])[1]::int[];

  

--select rows to array
SELECT ARRAY(select "Id" FROM ent."Enterprise" )
SELECT ARRAY(select "Id" FROM ent."Enterprise" )::int[]
--array foreach
DO
$do$
DECLARE
m varchar[];
arr varchar[] := array[['key1','val1'],['key2','val2']];
BEGIN
FOREACH m SLICE 1 IN ARRAY arr
LOOP
RAISE NOTICE 'another_func(%,%)',m[1], m[2];
END LOOP;
END
$do$
DO
$do$
DECLARE
m varchar[];
arr varchar[] := array['key1','val1'];
BEGIN
FOREACH m SLICE 1 IN ARRAY arr
LOOP
RAISE NOTICE 'another_func(%)',m;
END LOOP;
END
$do$

  

--一维数组
DO
$do$
DECLARE
m int;
arr int[] := ARRAY(select "Id" FROM ent."Enterprise" )::int[];
BEGIN
FOREACH m IN ARRAY arr
LOOP
RAISE NOTICE 'another_func(%)',m;
END LOOP;
END
$do$

   

--each insert from select
DO
$do$
DECLARE
m int;
arr int[] := ARRAY(select "Id" FROM "Enterprise" )::int[];
BEGIN
FOREACH m IN ARRAY arr
LOOP
INSERT INTO "Wallet"("Id")
VALUES (m);
  RAISE NOTICE 'another_func(%)',m;
END LOOP;
END
$do$

  

多维数组

WITH data AS (
SELECT '{
{9,"12345"}
, {9,aedrftgy}
, {11,qwedrftgyh}
,{10,qertg}
,{12,qwedfg}
,{9,wedrftgh}
,{13,qwerftgh}
,{8,wertyu}
}'::text[] AS arr
)
SELECT
arr[i][1] AS aspect,
arr[i][2] AS preference
FROM
data,
generate_subscripts((SELECT arr FROM data), 1) i
;
DO
$do$
DECLARE
r record;
r2 record;
n int;
channelId integer;
v_MaxId integer;
platfromSettlementParty text;
v_roles text[] := '{
{9,"zfhcnc''gdthfh"}
, {9,123有限公司}
, {11,3456有限公司}
,{10,467有限公司}
,{12,5678有限公司}
,{9,34567司}
,{13,3456公司}
,{8,23456公司}
}';
BEGIN
for r in (
WITH data AS (
SELECT v_roles::text[] AS arr
)
SELECT * from (SELECT arr[i][1] AS aspect, arr[i][2] AS preference
FROM data, generate_subscripts((SELECT arr FROM data), 1) i) t2
)
LOOP
channelId=CAST(coalesce(r."aspect", '0') AS integer);
platfromSettlementParty=r."preference";
Select max("Id")+1 into v_MaxId from ent."rrte";
IF NOT EXISTS(Select * from ent."rrte" where ent."rrte"."PlatfromSettlementParty"=platfromSettlementParty and ent."rrte"."ChannelId"=channelId limit 1) then
INSERT INTO ent."rrte"("Id",""ChannelId", "PlatfromSettlementParty")
VALUES (v_MaxId,channelId, platfromSettlementParty);
end if;
END LOOP; END;
$do$;

  

  

  

DO
$do$
DECLARE r record;
r2 record;
er record;
eachr record;
n int;
ci integer;
v_MaxId integer;
v_pi integer;
v_bi integer;
v_ci integer;
v_at integer;
psp text;
v_roles text[] := '{
{达有,9,百司,1,''}
{顾问,8,上海,2,''}
}';
BEGIN for r in (
WITH data AS (
SELECT v_roles::text[] AS arr
)
SELECT arr[i][1] AS en, arr[i][2] AS ci, arr[i][3] AS psp,arr[i][4] AS at,arr[i][5] AS am
FROM data, generate_subscripts((SELECT arr FROM data), 1) i
)
LOOP
psp= r."psp";
v_at=CAST(coalesce(r."at", '0') AS integer);
Select max("Id")+1 into v_MaxId from ent."BA"; IF EXISTS(select "Id" from ent."A" as platform WHERE platform."PSP"=r."psp" limit 1)then
select "Id" into v_pi from ent."A" as platform WHERE platform."PSP"=r."psp" limit 1; IF EXISTS(SELECT business."Id" FROM ent."B" as business
INNER join ent."E" as enterprise on business."EI" = enterprise."Id"
where business."BU"=2 and ( enterprise."N"=r."en" or enterprise."EN"=r."en") limit 1)then SELECT business."Id" into v_bi FROM ent."B" as business
INNER join ent."E" as enterprise on business."BI" = enterprise."Id"
where business."BU"=2 and ( enterprise."N"=r."en" or enterprise."EN"=r."en") limit 1; IF NOT EXISTS(
SELECT account."Id", account."BI", account."PI", account."CI", account."AT", account."AM"
FROM ent."BA" as account where account."BI"=v_bi and account."AI"=v_pi
limit 1) then INSERT INTO ent."BA"("Id","BI", "AI", "AT", "AM")
VALUES (v_MaxId, v_bI, v_pt, v_at,r."am");
ELSE
UPDATE ent."BA" as account
SET "AT"=v_at, "AM"=r."am"
where account."BId"=v_bi and account."AId"=v_pi;
end if;
end if;
end if;
END LOOP; END;
$do$; UPDATE ent."BA"
SET "AM"=''
where "AM"='''' ;

  

  

  

  

postgresql:array & foreach的更多相关文章

  1. 为什么 array.foreach 不支持 async/await

    一.背景 react 项目中,渲染组件时,显示的数据一直有问题,本来以为是 react 组件的问题,后来才发现罪魁祸首在 fetch 数据的过程,因为我用了 async/await ,而却搭配了 fo ...

  2. Array.forEach原理,仿造一个类似功能

    Array.forEach原理,仿造一个类似功能 array.forEach // 设一个arr数组 let arr = [12,45,78,165,68,124]; let sum = 0; // ...

  3. 如何在 Array.forEach 中正确使用 Async

    本文译自How to use async functions with Array.forEach in Javascript - Tamás Sallai. 0. 如何异步遍历元素 在第一篇文章中, ...

  4. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  5. 【前端_js】array.forEach和$.each()及$().each()的用法与区别

    1.$.each():方法是jQuery中的方法,用于遍历数组或对象.用法:$.each(array,function(index,value){...}),有两个参数,第一个为待遍历的数组或对象,第 ...

  6. PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】

    前言 PostgreSQL 是世界公认的功能最强大的开源数据库,除了基础数据类型 int4/int8/varchar/numeric/timestamp 等数据类型,还支持 int4[]/int8[] ...

  7. YUI Array 之each| forEach(遍历)

    1. yui-each原码: 遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn ...

  8. 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight

    做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...

  9. Javascript数组Array的forEach方法

    Javascript数组Array的forEach扩展方法 forEach是最常用到的数组扩展方法之一,相当于参数化循环数组,它简单的在数组的每一个元素上应用传入的函数,这也意味着只有存在的元素会被访 ...

随机推荐

  1. 实战http切换成https

    Server端使用Nginx + Tomcat Niginx SSL on Tomcat SSL non 步骤: 1.修改代码,将外部引用的http js css 文件修改为https,若外部链接不支 ...

  2. 2017-2018-1 20155228 《信息安全系统设计基础》第六周学习总结&课下作业

    20155228 2017-2018-1 <信息安全系统设计基础>第六周学习总结&课下作业 教材学习内容总结 异常及其种类 异常可以分为四类:中断(interrupt) ,陷阱(t ...

  3. Nginx配置基础-location

    location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配,不区分大小写^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location.= 进行普通 ...

  4. [转载]oracle函数listagg的使用说明

    工作中经常遇到很多需求是这样的,根据条件汇总某些字段,比如我遇到的是,我们公司有三个投资平台,同一个客户拿手机号在三个平台都注册了,但注册过的用户名不一样,显示的时候需要根据手机号显示所有注册过的名称 ...

  5. node.js的on、emit、off封装

    //绑定事件.触发事件和移除事件 //绑定事件 //on(eventName,cb){} //第一步判断当前事件是否存在,如果不存在则初始化:key:[],然后在将回调函数添加到数据中去 let ev ...

  6. python的ws库功能,实时获取服务器ws协议返回的数据

    # -*- coding:utf-8 -*- ''' 模块下载,帮助地址:https://github.com/liris/websocket-client#readme 模块:websocket-c ...

  7. 利用Python实现多线程聊天功能

    #-*- coding:utf-8 -*- from threading import Thread from socket import * #1.实现接收消息 def recvDate(): wh ...

  8. sqlalchemy学习笔记

    https://segmentfault.com/a/1190000006949536

  9. Linux学习笔记之Linux运行脚本时 $'\r' 错误

    1.Windows上操作 用notepad++编译器打开脚本,编辑->文档格式转换->转换为UNIX格式,然后保存. 重新上传.运行,问题解决 2.Linux上操作 用vi/vim命令打开 ...

  10. 前端基础小标签3 H5新标签

    第二部分H5的新标签一.<!-- mark标签             1.用于显示页面中需要重点关注的内容,就像看书需要用荧光笔划重点一样             2.浏览器通常会用黄色显示m ...