• 使用postgrasql存储过程循环插入数据


    今天在一个项目中,需要创建大量的数据,由于刚好改项目使用的数据库是postgrasql。之前对其他数据库倒是有了解,但是postgrasql的了解却是不多。知道有存储过程的这一个概念,在网上查找资料,在postgrasql中没有发现有CREATE OR REPLACE procedure的语法。走了好多弯路才知道,原来只能用FUNCTION 来实现,在使用过程中还发现还有好多语法和其他数据库不同,比方说调用只能用select语句实现。看来,接下来需要对postgrasql语法,进行研究研究了>_<'''。

    以下是本次使用的脚本,写此随笔,以供他人参考。

    CREATE OR REPLACE FUNCTION addvalue(recordnum bigint, int_day bigint)
    RETURNS void AS
    $BODY$
    declare i integer;
    declare j integer;
    BEGIN
    i:=1;
    j:=1;
    for j in 1..int_day loop
    for i in 1..recordNum loop
    insert into acc_transaction(id,unique_key,log_id,create_time,event_time,pin,area_name,card_no,dev_id,dev_sn,dev_alias,verify_mode_no,verify_mode_name,event_no,event_name,event_point_type,event_point_id,event_point_name,reader_state,reader_name)
    values(nextval('seq_acc_transaction'),CONCAT(CONCAT(CONCAT('70000000002',to_char(j,'FM999999')),'_'),to_char(i,'FM999999')),i,to_timestamp(CONCAT(CONCAT('2015-07-',to_char(j,'FM999')),' 02:01:33'),'YYYY-MM-DD HH:MI:SS'),to_timestamp(CONCAT(CONCAT('2015-07-',to_char(j,'FM999')),' 02:01:33'),'YYYY-MM-DD HH:MI:SS'),'1044','Area Name','395747',3,CONCAT('70000000002',to_char(j,'FM999999')),'192.168.4.12',6,'Card or Fingerprint',30,'Password Error',0,43,'192.168.4.12-1',1,'192.168.4.12-1');
    end loop;
    end loop;
    end
    $BODY$
    LANGUAGE plpgsql VOLATILE

  • 相关阅读:
    在wampserver环境下配置多个版本的PHP支持
    Jetbrains全系列完美破解--------亲测可用
    python学习笔记1:元组
    python学习笔记0:列表
    入坑:分享几个优秀的编程博客
    java集合-EnumMap与EnumSet
    java集合-TreeSet
    java集合-TreeMap
    java集合-HashMap(JDK1.8)
    java集合-HashSet
  • 原文地址:https://www.cnblogs.com/zktesterhome/p/5048776.html
Copyright © 2020-2023  润新知