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是最常用到的数组扩展方法之一,相当于参数化循环数组,它简单的在数组的每一个元素上应用传入的函数,这也意味着只有存在的元素会被访 ...
随机推荐
- 举例说明Unicode 和UTF-8之间的转换
1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...
- tft屏图像文字一起显示
2010-05-04 21:06:00 M16内部flash只有16k,要做数码相框,只能用usart通信了.明天继续研究.
- AtCoder Beginner Contest 045 C - たくさんの数式 / Many Formulas
Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given a string ...
- What Would you Find out about MS908CV ?
The Autel MaxiSYS commercial car diagnostics scan device, No. MS908CV, performs increased technique ...
- java内存泄漏与内存溢出
https://www.cnblogs.com/panxuejun/p/5883044.html 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out o ...
- 一文看懂显示关键材料之彩色滤光片(Color Filter)
http://www.sohu.com/a/219398623_119960 液晶显示器的背光源发出的白光,而想要获得彩色显示,必须依靠显示关键材料-彩色滤光片. 图片来源:网络公开资料 什么是彩色滤 ...
- IntelliJ IDEA. Debug模式
资料收集: https://www.bilibili.com/video/av6749471/?p=16 eclipse debug模式. 基础 Intellij Idea--Debug使用 Inte ...
- MySQL 跨库主从
一个线上需求,要求主从两库的数据库名字不一样, 在从库上做如下配置: replicate-rewrite-db=DB_1->DB_1_online replicate-rewrite-db=DB ...
- fjwc2019 D1T3 不同的缩写(dinic+trie+dfs)
#180. 「2019冬令营提高组」不同的缩写 乍看之下没有什么好的方法鸭.......于是考虑暴力. 长度?二分似乎可行. 于是我们二分最长子串的长度(设为$len$),蓝后暴力查找. 先在每个串内 ...
- jackson/fastjson、mybatis、mysql date/datatime/timestamp、java Date/Timestamp关系详解
jackson/fastjson序列化/反序列化: 默认情况下,jackson/fastjson将java Date/Timestamp类型序列化为时间戳,也就是1970年1月1日0点以来的毫秒数.如 ...