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是最常用到的数组扩展方法之一,相当于参数化循环数组,它简单的在数组的每一个元素上应用传入的函数,这也意味着只有存在的元素会被访 ...
随机推荐
- AmiGO2:在线浏览和查询GO信息的利器
GO数据库的信息是非常庞大的,为了有效的检索和浏览GO数据库的信息,官方提供了AmiGO, 可以方便的浏览,查询和下载对应信息,官网如下 http://amigo.geneontology.org/a ...
- 批量下载网站图片的Python实用小工具(下)
引子 在 批量下载网站图片的Python实用小工具 一文中,讲解了开发一个Python小工具来实现网站图片的并发批量拉取.不过那个工具仅限于特定网站的特定规则,本文将基于其代码实现,开发一个更加通用的 ...
- linux常用命令:cat 命令
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- 限制访问次数例子和Ajax的some
-- 限制访问次数作业解答 -- -- urls.py 里-- from django.conf.urls import url from django.contrib import admin fr ...
- emmm
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; st ...
- Codeforce 507B - Amr and Pins
Amr loves Geometry. One day he came up with a very interesting problem. Amr has a circle of radius r ...
- python之运算符与基本数据类型
1.开发工具:IDE pycharm(推荐).eclipse 2.运算符 结果是值 算数运算 a = 10 * 10 赋值运算 a = a + 1 a+=1 结果是布尔值 比较运算 a = 1 ...
- The Little Prince-12/14
The Little Prince-12/14 世界上有那么多的玫瑰花,但是只有你们是一直陪伴在我身边,我们相互灌溉. ————世界上有那么多的人,但是只有你们是一直陪伴在我身边,我们相互关心. “在 ...
- 关于JSONObject和JSONArray所需要的jar
jakarta commons-lang 2.5 jakarta commons-beanutils 1.8.0 jakarta commons-collections 3.2.1 jakarta c ...
- shell 冒号
: ${TEST_LOOP:='1'} 如果不在前面加上:(冒号)命令,那么就会把${TEST_LOOP:='1'}本身当做一个命令来执行,报错是肯定的. [root@node56 ~]# : abc ...