postgresql:array & foreach
--数组:
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的更多相关文章
- 为什么 array.foreach 不支持 async/await
一.背景 react 项目中,渲染组件时,显示的数据一直有问题,本来以为是 react 组件的问题,后来才发现罪魁祸首在 fetch 数据的过程,因为我用了 async/await ,而却搭配了 fo ...
- Array.forEach原理,仿造一个类似功能
Array.forEach原理,仿造一个类似功能 array.forEach // 设一个arr数组 let arr = [12,45,78,165,68,124]; let sum = 0; // ...
- 如何在 Array.forEach 中正确使用 Async
本文译自How to use async functions with Array.forEach in Javascript - Tamás Sallai. 0. 如何异步遍历元素 在第一篇文章中, ...
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- 【前端_js】array.forEach和$.each()及$().each()的用法与区别
1.$.each():方法是jQuery中的方法,用于遍历数组或对象.用法:$.each(array,function(index,value){...}),有两个参数,第一个为待遍历的数组或对象,第 ...
- PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】
前言 PostgreSQL 是世界公认的功能最强大的开源数据库,除了基础数据类型 int4/int8/varchar/numeric/timestamp 等数据类型,还支持 int4[]/int8[] ...
- YUI Array 之each| forEach(遍历)
1. yui-each原码: 遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn ...
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...
- Javascript数组Array的forEach方法
Javascript数组Array的forEach扩展方法 forEach是最常用到的数组扩展方法之一,相当于参数化循环数组,它简单的在数组的每一个元素上应用传入的函数,这也意味着只有存在的元素会被访 ...
随机推荐
- 基于word2vec训练词向量(二)
转自:http://www.tensorflownews.com/2018/04/19/word2vec2/ 一.基于Hierarchical Softmax的word2vec模型的缺点 上篇说了Hi ...
- Linux基础命令---traceroute追踪路由
traceroute traceroute指令输出到目标主机的路由包.Traceroute跟踪从IP网络到给定主机的路由数据包.它利用IP协议的生存时间(TTL)字段,并试图在通往主机的路 ...
- mysql-day06
##视图 - 什么是视图:在数据库中存在多种对象,表和视图都是数据库中的对象,创建视图时名称不能和表重名,视图实际上就代表一段sql查询语句,也可以理解成视图是一张虚拟的表,此虚拟表中的数据会随着原表 ...
- ORM some
1 -- 增 models.表名(类).objects.create(字段1=值,字段2=值) 查 models.表名(类).objects.get(pk = 3) models.表名(类).obje ...
- 常用linux命令行
1.ls命令 ls -a 列出目录所有文件,包含以.开始的隐藏文件 ls -A 列出除.及..的其它文件 ls -r 反序排列 ls -t 以文件修改时间排序 ls -S 以文件大小排序 ls -h ...
- SQL Server 2008 R2 常用系统函数学习
/******************************************* * 聚合函数 *******************************************/ SEL ...
- ubuntu 常见命令整理
SSH 查看ssh服务的进程是否已经开启ps -e | grep ssh 安装ssh服务组件sudo apt-get install openssh-server 服务启动和关闭 方法1:servic ...
- Doing Homework HDU - 1074
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...
- Firefox创建firefoxprofile
我们自动化测试的时候,有时不需要图片加载出来,提高浏览器加载速度,从而提高脚本的执行速度.另外在一些网络比较差的环境下,禁用css.图片等加载可以提高访问速度 方法: 1.创建自己的firefoxpr ...
- jQuery知识总结(转)
原文:http://fwhyy.com/2013/04/jquery-knowledge-summary/ 这篇文章在于筛选器的简单例子,让人一看就懂代码的作用 20170223 前言 jQuery一 ...