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是最常用到的数组扩展方法之一,相当于参数化循环数组,它简单的在数组的每一个元素上应用传入的函数,这也意味着只有存在的元素会被访 ...
随机推荐
- django models数据库操作
一.数据库操作 1.创建model表 基本结构 1 2 3 4 5 6 from django.db import models class userinfo(models.M ...
- sql server和oracle数据库
sql server和oracle数据库安装按照官方教程即可:以及他们相应的管理工具,sql server management studio自带的,oracle的管理工具PLSQL需要单独下载安装, ...
- mybatis源码解析2---SqlSessionFactoryBuilder、SqlSessionFactory解析
1.先看一个mybatis最简单的Demo String resource = "mybatis-config.xml"; //1.流形式读取mybatis配置文件 InputSt ...
- Future复习笔记
1. Future就是对于具体的Runnable或者Callable任务的执行结果进行取消.查询是否完成.获取结果.必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果. Future类 ...
- mongoDB启动前的系统设置,解决部分Warning问题
1.多核心系统资源分配问题 2017-10-13T17:57:02.650+0800 I CONTROL [initandlisten] ** WARNING: You are running on ...
- 那个写书教你交易期权的人James Cordier爆仓了
那个写书教你交易期权的人James Cordier爆仓了 11月15日,James Cordier掌管的期权交易公司OptionSellers.com通过邮件告知投资者,其公司管理的账户遭遇了毁灭性的 ...
- 安装指定版本capistrano
1.ruby安装 #yum install -y openssl-devel readline-devel zlib-devel git #git clone https://github.com/s ...
- Hadoop学习笔记之三:DataNode
DataNode对ClientDatanodeProtocol.InterDatanodeProtocol两个协议接口进行了实现,通过ipc::Server向Client.其它DN提供RPC服务(参见 ...
- 前端框架VUE----指令
一.什么是VUE? 它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 二.怎么使用VUE? 1.引入vue.js 2.展示HTML <div id=" ...
- mysql启动、关闭与登录
按照上述三篇随笔中的方法安装mysql,其启动.关闭和登录方法如下. mysql启动基本原理:/etc/init.d/mysqld是一个shell启动脚本,启动后最终会调用mysql\bin\mysq ...