【性能脚本】psql数据库存储过程构造方法

1、数据库版本:PostgreSQL 14.5
2、psql时间函数:

-- 日期:下个月当天 2023-03-10

SELECT to_date((to_char(( now() + interval '1 month'),'YYYY-MM-DD')),'YYYY-MM-DD')

-- 日期:下个月第一天 2023-03-01

SELECT to_date((to_char(( now() + interval '1 month'),'YYYY-MM-01')),'YYYY-MM-DD')

-- 日期:上个月第一天 2023-01-01

SELECT to_date((to_char(( now() + interval '-1 month'),'YYYY-MM-01')),'YYYY-MM-DD')

-- 日期:本月底 2023-02-28

SELECT (to_date((to_char(( now() + interval '1 month'),'YYYY-MM-01')),'YYYY-MM-DD')-1)::date

SELECT CAST (now() AS timestamp)

SELECT now(),current_timestamp,current_date,current_time,localtime,loca1ltimestamp;

select now() + interval '1 min';

select now() + interval '1 days';

select now() + interval '1 month';

select now() + interval '1 years';

select to_char(now(),'yyyymmddHHMMSS')

3、psql存过实例:

CREATE OR REPLACE PROCEDURE "public"."histable"()

AS BODYBEGIN

do $$

declare

i INTEGER := 10000;

vehicleId VARCHAR(36);

checkintime TIMESTAMP;

checkouttime TIMESTAMP;

declarationNo VARCHAR(50);

licensePlateNo VARCHAR(100);

licensePlateNoBack VARCHAR(100);

taskid VARCHAR(32);

measurevehicleId VARCHAR(32);

seqid VARCHAR(32);

uuId VARCHAR(36);

BEGIN

WHILE i <= 109999 LOOP

i = i + 1;

SELECT INTO vehicleId to_char(now()+ interval '-1 month','yyyymmdd')||CEIL(random()*(1000000000000000-1)+1);

SELECT INTO measurevehicleId 'M'||ceil(random()*(1000000000000000-1)+1);

SELECT INTO checkintime CAST(now()+ interval '-1 month' AS timestamp);

SELECT INTO checkouttime CAST(now()+ interval '-1 month' + '1 min' AS timestamp);

SELECT INTO declarationNo 'DN'||floor(random()*(1000000000-1)+1);

SELECT INTO licensePlateNo 'la' || ceil(random()*(10000000000-1)+1);

SELECT INTO licensePlateNoBack 'lb' || ceil(random()*(10000000000-1)+1);

SELECT INTO taskid 'Alakoll'||ceil(random()*(100000000000000-1)+1);

SELECT INTO uuId 'UUID'||ceil(random()*(100000000000000-1)+1);

-- SELECT INTO uuId gen_random_uuid();

-- vehicleId := CEIL(random()*(1000000000000000-1)+1);

-- measurevehicleId := 'M'||ceil(random()*(1000000000000000-1)+1);

-- checkintime := CAST(now() AS timestamp);

-- checkouttime := CAST(now() + '1 min' AS timestamp);

-- declarationNo := floor(random()*(1000000000-1)+1);

-- licensePlateNo := 'la' || ceil(random()*(10000000000-1)+1);

-- licensePlateNoBack := 'lb' || ceil(random()*(10000000000-1)+1);

-- taskid := 'Alakoll'||ceil(random()*(10000000000-1)+1);

-- seqid := to_char(now(),'yyyymmddHHMMSS');

-- uuId := gen_random_uuid();

INSERT INTO "public"."VEHICLE_INSPECTED_HIS" ("ID", "ALARM_LOGISTICS", "ALARM_TIMEOUT", "ANIMAL_CONCLUSION_TYPE", "ANIMAL_OPERATION_USER_NAME", "ARC_COUNT", "ASYCUDA_CODE", "BLACK_VEHICLE", "BUSINESS_TYPE", "CHECK_IN_ARRIVAL_TIME", "CHECK_IN_TIME", "CHECK_OUT_ARRIVAL_TIME", "CHECK_OUT_TIME", "COMBINE_STATUS", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CONTAINER_NUMBER", "CREATION_TIME", "DC_CONCLUSION_TIME", "DECLARATION_NUMBER", "DEST_PORT_CODE", "DEST_PORT_NUMBER", "DIRECTION", "ENTRY_TUNNEL", "HEALTH_CONCLUSION_TYPE", "HEALTH_OPERATION_USER_NAME", "INSPECTED_PORT_CODE", "INSPECTED_PORT_NUMBER", "JOINT_INSPECTION_TIME", "LICENSE_PLATE_NUMBER", "LICENSE_PLATE_NUMBER_BACK", "LOCKED", "LOCKED_TIME", "LOCKED_USER_ACCOUNT", "LOCKED_USER_ID", "LOCKED_USER_NAME", "LOGISTICS", "LOGISTICS_FINISH", "LOGISTICS_REQUEST", "NORMAL_END", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "ORIGIN_PORT_CODE", "ORIGIN_PORT_NUMBER", "ORIGIN_VEHICLE_ID", "PI_CONCLUSION_TIME", "PLANT_CONCLUSION_TYPE", "PLANT_OPERATION_USER_NAME", "LANE_NUMBER", "SITE", "RELEASE", "RELEASE_CONFIRM_TIME", "RFID_NUMBER", "RISK_LEVEL", "SUSPICION_MARK", "SYN_STATUS", "TRAILER", "TRANSFERED_CENTRE", "TRANSIPORT_CONCLUSION_TYPE", "VEHICLE_HEIGHT", "VEHICLE_LENGTH", "VEHICLE_STATUS", "VEHICLE_TYPE", "VEHICLE_WEIGHT", "VEHICLE_WIDTH", "XRAY_PRIP_SUCCESS", "XRAY_SAVE_PATH", "XRAY_SCAN", "XRAY_SCAN_END_TIME", "XRAY_SCAN_ID", "XRAY_SCAN_START_TIME", "DCIN_MODE", "PRD_ALARM", "SENDED_CENTRE", "ADDED_PIN", "XRAY_SCAN_STATUS", "DCIN_CONCLUSION_TYPE", "PIN_CONCLUSION_TYPE", "ARC_FACTOR_TYPE", "SEIZED_POSITION", "SEIZED_GOODS", "ANIMAL_LOCK_USER_ACCOUNT", "HEALTH_LOCK_USER_ACCOUNT", "MEASURE", "MEASURE_FINISH", "PLANT_LOCK_USER_ACCOUNT", "SELECT_LANE", "TASK_ID", "WHITE_LIST", "XRAY_SCAN_FINISH", "PLAN_LANENUMBER", "DC_EXPORT_COUNT", "SCAN_MODE", "PLAN_LANENUMBER_SCAN_MODE", "PRINT_COUNT", "SCAN_RESULT") VALUES (vehicleId, 'f', 'f', 'PASSED', 'wgp', NULL, NULL, 'f', 'EXPORT', checkintime, checkintime, NULL, checkouttime, 'f', 'No Suspect', 'RELEASE', 'SIIU8005669,SIIU6033094', checkintime, checkouttime, declarationNo, NULL, NULL, 'EXPORT', 'f', 'PASSED', 'wgp', 'Alakol', '10002', NULL, licensePlateNo, licensePlateNoBack, 'f', NULL, NULL, NULL, NULL, 'f', 'f', 'f', 't', checkouttime, 'wgp', '7a4ae50c-c255-4206-95f2-9aaef19e816f', 'wgp', 'Alakol', '10002', NULL, NULL, 'PASSED', 'wgp', '1', 'ICIA', 't', checkouttime, NULL, NULL, 'f', 'f', 'f', 'f', 'PASSED', NULL, NULL, 'FINISHCHECKED', 'TRUCK', NULL, NULL, 't', '/nii/97201MB02/2021/0608/97201MB0220210608003/', 't', checkintime, '97201MB0220210608003', checkintime, 'LOCAL', 'f', NULL, NULL, 'f', 'PASS', NULL, NULL, NULL, NULL, NULL, NULL, 't', 't', NULL, 'SCAN', taskid, NULL, 't', '1', 0, 'FS', 'J', 0, 'SUCCESS');

INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'a', '通过', 'PASSED', checkintime, checkouttime, 'ANIMALCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);

INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'b', '通过', 'PASSED', checkintime, checkouttime, 'PLANTCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);

INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'c', '通过', 'PASSED', checkintime, checkouttime, 'HEALTHCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);

INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'d', '遣返', 'REPATRIATE', checkintime, checkouttime, 'TRAFFICCONFIRM', licensePlateNo, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);

END LOOP;

END $$;

ENDBODY

LANGUAGE plpgsql

相关推荐
大波V56 分钟前
设计模式-参考的雷丰阳老师直播课
java·开发语言·设计模式
计算机-秋大田12 分钟前
基于微信小程序的平安驾校预约平台的设计与实现(源码+LW++远程调试+代码讲解等)
java·spring boot·微信小程序·小程序·vue·课程设计
《源码好优多》24 分钟前
基于Java Springboot旅游信息推荐系统
java·spring boot·旅游
岁月无声code25 分钟前
Spring Boot 牛刀小试 org.springframework.boot:spring-boot-maven-plugin:找不到类错误
java·spring boot·github
夏小花花29 分钟前
postgresql 创建序列
数据库·postgresql
Allen Bright29 分钟前
Redis介绍
数据库·redis·缓存
engchina36 分钟前
Oracle ADB 导入 BANK_GRAPH 的学习数据
数据库·学习·oracle·graph
不爱学习的YY酱38 分钟前
【计网不挂科】计算机网络第二章< 物理层 >习题库(含答案)
java·数据库·计算机网络
CCSBRIDGE44 分钟前
sql文件
数据库·oracle
柯南二号1 小时前
HarmonyOS ArkTS 下拉列表组件
前端·javascript·数据库·harmonyos·arkts