当前位置:网站首页>pgsql_ UDF01_ jx
pgsql_ UDF01_ jx
2022-06-26 08:40:00 【Algorithmic Pilgrim】
- Need new sequence. Because some tables already have data , At this time , New sequence The starting position of cannot be from 1 Start .
CREATE OR REPLACE FUNCTION create_sequence(sequence_name character varying, table_name character varying, column_name character varying)
–sequence_name It needs to be created sequence name
–table_name yes sequence Action table
–column_name It is the column value that counts the data volume of the current table
RETURNS bigint AS
B O D Y BODY BODY
DECLARE
startNum int8;
createsequence “varchar” := ‘’;
selectsequence “varchar” := ‘’;
BEGIN
-- First, get how much data has been saved in the face-to-face table
selectsequence := 'select COALESCE(max('||column_name||'), 0) + 1 FROM '|| table_name ||'';
-- Put the query results into startNum in
execute selectsequence into startNum;
-- Statement establish sequence The sentence of .
createsequence := 'CREATE SEQUENCE '||sequence_name||'
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START '|| startNum ||'
CACHE 1';
-- Execution creation sequence Of SQL sentence
execute createsequence;
RETURN startNum;
END;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION create_sequence(character varying, character varying, character varying) OWNER TO postgres;
- demand , These two lines of data need to be Add each column .
data
2.1 First will string Convert to array . Such as 1,2,3,4,5 Convert to [1,2,3,4,5]
CREATE OR REPLACE FUNCTION tools_str2array(_originstr text)
RETURNS integer[] AS
B O D Y BODY BODY
declare _cindex INTEGER;
declare _arrIndex INTEGER;
DECLARE _arr_str int4[];
DECLARE _tmp_str VARCHAR(10);
DECLARE _delimeter VARCHAR(1);
BEGIN
_arrIndex:=1;
_cindex:=1;
_delimeter:=’,’;
–_strres:=_strres||‘ The original string is :’||_originStr;
while _cindex<“length”(_originStr) loop
–_strres :=_strres||’【 What is this? ?】’||split_part(_originStr, _delimeter, _arrIndex);
_tmp_str:=split_part(_originStr, _delimeter, _arrIndex);
if “character_length”(_tmp_str)<1 then
exit;
end if;
_arr_str:=_arr_str|| CAST(_tmp_str as int4);
_arrIndex:=_arrIndex+1;
END loop;
return _arr_str;
end;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION tools_str2array(text) OWNER TO postgres;
2.2. New aggregate function .
CREATE AGGREGATE hot_map_test(
BASETYPE = TEXT,– The input values
SFUNC = csk_test_start,– The function that was originally executed
STYPE = TEXT[],– The output type
FINALFUNC = count_hot_map– The last function executed
);
2.3 New calculation function The result of the calculation is
CREATE AGGREGATE count_test(
BASETYPE = TEXT,
SFUNC = csk_test_start,
STYPE = TEXT[],
FINALFUNC = count_hot_map
);
CREATE OR REPLACE FUNCTION csk_test_start(a text[], s text)
RETURNS text[] AS
B O D Y BODY BODY
BEGIN
RETURN a || s;
END;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION csk_test_start(text[], text) OWNER TO postgres;
CREATE OR REPLACE FUNCTION count_hot_map(text[])
RETURNS integer[] AS
B O D Y BODY BODY
DECLARE
startNum int8;
spilt_sql “varchar” := ‘’;
selectsequence “varchar” := ‘’;
temp_array int4[];
result_array int4[];
BEGIN
for index in 1..array_length($1 , 1) loop
spilt_sql = 'select tools_str2Array('''||$1[index]||''')';
execute spilt_sql into temp_array;
for array_items_index in 1..array_length(temp_array, 1) loop
if result_array[array_items_index] is null then
result_array[array_items_index] = 0;
end if;
result_array[array_items_index] = result_array[array_items_index] + temp_array[array_items_index];
end loop;
end loop;
RETURN result_array;
END;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION count_hot_map(text[]) OWNER TO postgres;
边栏推荐
- 加密的JS代码,变量名能破解还原吗?
- Swift code implements method calls
- FFmpeg音视频播放器实现
- Addition of attention function in yolov5
- 批量执行SQL文件
- Vs2019-mfc setting edit control and static text font size
- STM32 encountered problems using encoder module (library function version)
- Discrete device ~ diode triode
- STM32 based d18s20 (one wire)
- CodeBlocks integrated Objective-C development
猜你喜欢

First character that appears only once

Can the encrypted JS code and variable name be cracked and restored?

Implementation of ffmpeg audio and video player

HEVC学习之码流分析

Idea automatically sets author information and date

Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data

Discrete device ~ resistance capacitance

opencv學習筆記三
![[unity mirror] use of networkteam](/img/b8/93f55d11ea4ce2c86df01a9b03b7e7.png)
[unity mirror] use of networkteam

1GHz active probe DIY
随机推荐
(2) Buzzer
关于极客时间 | MySQL实战45讲的部分总结
Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
1GHz active probe DIY
Relationship extraction -- casrel
Timer code guide in optee
Transformers loading Roberta to implement sequence annotation task
Learn signal integrity from zero (SIPI) - (1)
Calculation of decoupling capacitance
Is it safe to open an account in flush,
ZLMediaKit推流拉流测试
Tokenizer description in Bert
Batch modify file name
监听iPad键盘显示和隐藏事件
STM32 project design: smart home system design based on stm32
See which processes occupy specific ports and shut down
Assembly led on
SOC的多核启动流程详解
STM32 project design: an e-reader making tutorial based on stm32f4
Application of wireless charging receiving chip xs016 coffee mixing cup