1. create extension IF NOT EXISTS "uuid-ossp" ;
  2. --select uuid_generate_v4();
  3. --select current_timestamp;
  4.  
  5. --select * from (select *,row_number() over(partition by merchant_id order by create_time desc) as num from merchants.merchant_money_record) as temp_tb1 where num=1;
  6.  
  7. CREATE OR REPLACE FUNCTION for_loop_through_query(
  8. )
  9. RETURNS VOID AS $$
  10. DECLARE
  11. rec RECORD;
  12. merchantid uuid;
  13. balance NUMERIC;
  14. record_remark VARCHAR;
  15. account_entity_id uuid;
  16. account_id uuid;
  17. transaction_flow_id INT;
  18. transaction_flow_detail_id INT;
  19. transaction_time TIMESTAMP;
  20. BEGIN
  21. DELETE FROM "AccountBook".accounting_entity;
  22. DELETE FROM "AccountBook".account;
  23. DELETE FROM "AccountBook".callback_log;
  24. DELETE FROM "AccountBook".transaction_flow;
  25. DELETE FROM "AccountBook".transaction_flow_detail;
  26. FOR rec IN select * from (select *,row_number() over(partition by merchant_id order by create_time desc) as num from merchants.merchant_money_record) as temp_tb1 where num=1
  27. LOOP
  28. account_entity_id := uuid_generate_v4();
  29. account_id := uuid_generate_v4();
  30. --transaction_flow_id := uuid_generate_v4();
  31. merchantid := rec.merchant_id;
  32. balance := rec.current_amount;
  33. record_remark := rec.record_remark;
  34. transaction_time := rec.transaction_time;
  35. INSERT INTO "AccountBook".accounting_entity ("id","type",system_id) VALUES (account_entity_id,1,merchantid);
  36. INSERT INTO "AccountBook".account ("id","accounting_entity_id","type","balance","createtime","currency_type","status","system_id") VALUES (account_id,account_entity_id,1,balance,current_timestamp,1,1,merchantid);
  37. INSERT INTO "AccountBook".transaction_flow ("type","business_num","occurrence_time","createtime","remark","accounting_entity_id") VALUES (7,record_remark,transaction_time,current_timestamp,'初始化余额',account_entity_id);
  38. transaction_flow_id := currval('"AccountBook".transaction_flow_id_seq');
  39. INSERT INTO "AccountBook".transaction_flow_detail ("transaction_flow_id","account_id","amount","trading_num","current_amount") VALUES (transaction_flow_id,account_id,balance,record_remark,balance);
  40. transaction_flow_detail_id := currval('"AccountBook".transaction_flow_detail_id_seq');
  41. RAISE NOTICE '%||%||%||%||%', merchantid,balance,account_entity_id,transaction_flow_id,transaction_flow_detail_id;
  42. END LOOP;
  43. END;
  44. $$ LANGUAGE plpgsql;
  45.  
  46. select for_loop_through_query();

参考https://pg.sjk66.com/

PG SQL funcation的更多相关文章

  1. sql差异

    类别 MS SQL Server My SQL PG SQL Oracle Access  自增  identity(1,1) auto_increment ALTER TABLE 'tableNam ...

  2. centos安装postgresql-10及操作

    安装postgresql: Linux启动: service postgresql start 创建用户: createuser username 创建数据库: createdb dbname -O ...

  3. 精读《sqorn 源码》

    1 引言 前端精读<手写 SQL 编译器系列> 介绍了如何利用 SQL 生成语法树,而还有一些库的作用是根据语法树生成 SQL 语句. 除此之外,还有一种库,是根据编程语言生成 SQL.s ...

  4. sqler 集成 terraform v0.12 生成资源部署文件

    terraform v0.12 发布了,有好多新功能的添加,包括语法的增强,新函数的引入,更好的开发提示 只是当前对于一些老版本的provider 暂时还不兼容,但是大部分官方的provider 都是 ...

  5. [转帖]PG语法解剖--基本sql语句用法入门

    PG语法解剖--基本sql语句用法入门 https://www.toutiao.com/i6710897833953722894/ COPY 命令挺好的 需要学习一下. 原创 波波说运维 2019-0 ...

  6. spark sql 查询hive表并写入到PG中

    import java.sql.DriverManager import java.util.Properties import com.zhaopin.tools.{DateUtils, TextU ...

  7. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  8. pg存储过程和sql语句块

    展E宝项目使用的是postgresql数据库,批量发送红包需求,需要采用存储过程来初始化红包记录数据. 创建存储过程语句有固定的架子,如下 CREATE OR REPLACE FUNCTION pub ...

  9. pg 和sql server 分别如何新建二进制数据库字段以及插入二进制数据的sql语句

    PG create table demo ( id int, name bytea ); Insert into demo (id,name)values(256,pg_read_binary_fil ...

随机推荐

  1. 2019年逾期率上升_24家头部P2P平台最新运营数据解读:8家近一年逾期率走势曝光

    python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...

  2. Sword cjson库函数使用

    /* cjson库的使用 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #includ ...

  3. 全面系统Python3入门+进阶-1-3 我为什么喜欢Python

    结束

  4. Spring cloud微服务安全实战-3-3 API安全机制之流控

    首先要保证你的服务是可用的,其中一个重要的手段就是流控.就是流量控制.比如我的系统每秒只能处理500个请求,那么多余的请求就拒绝掉.这样我的系统不会被压死 实际的开发中,所要面对的流控场景实际是非常复 ...

  5. django 中单独执行py文件修改用户名

    Python文件代码 import os import django # 在environ字典里设置默认Django环境,'xxxx.settings'指Django项目的配置文件 os.enviro ...

  6. mybatis typeHandler类型转换器

    typeHandler类型转换器 在JDBC中,需要在PreparedStatement对象中设置那些已经预编译过的SQL语句的参数.执行SQL后,会通过ResultSet对象获取得到数据库的数据,而 ...

  7. AnyCAD三维控件(转)

    在WinForm中可以方便的集成AnyCAD.Net三维控件,只需要以下几部即可完成. 一.添加DLL程序集 AnyCAD.Foundation.Net.dll AnyCAD.Presentation ...

  8. Nginx配置自定义的403页面

    1.开启nginx的状态码,虚拟主机配置中加入下边一段 location /nginx_status{ stub_status on; access_log off; } 或着在nginx的http模 ...

  9. Java连接MongoDB报错“java.lang.IllegalArgumentException: clusterListener can not be null”的解决办法

    我使用的MongoDB版本是3.6.9. 下面是一个很基础的示例代码,功能就是连接MongoDB: package com.zifeiy.snowflake.handle.etl.mongodb; i ...

  10. 使用sql语句创建和删除约束示例代码

    使用sql语句创建和删除约束  约束类型 主键约束(Primary Key constraint) --:要求主键列数据唯一,并且不允许为空.  唯一约束(Unique constraint) --: ...