当前位置:网站首页>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;
边栏推荐
- Microcontroller from entry to advanced
- Interpretation of x-vlm multimodal model
- Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
- WBC learning notes (II): practical application of WBC control
- STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial
- Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
- Analysis of internal circuit of operational amplifier
- 【Unity Mirror】NetworkTeam的使用
- SOC wireless charging scheme
- [postgraduate entrance examination] group planning exercises: memory
猜你喜欢

STM32 project design: temperature, humidity and air quality alarm, sharing source code and PCB

opencv學習筆記三

MySQL insert Chinese error

51 single chip microcomputer project design: schematic diagram of timed pet feeding system (LCD 1602, timed alarm clock, key timing) Protues, KEIL, DXP

Partial summary of 45 lectures on geek time MySQL

Relationship extraction --tplinker

Idea automatically sets author information and date

2020-10-17

Relation extraction model -- spit model

Whale conference one-stop intelligent conference system helps organizers realize digital conference management
随机推荐
Microcontroller from entry to advanced
Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
Use a switch to control the lighting and extinguishing of LEP lamp
MySQL insert Chinese error
(3) Dynamic digital tube
Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
js文件报无效字符错误
Assembly led on
软件工程-个人作业-提问回顾与个人总结
Mapping '/var/mobile/Library/Caches/com. apple. keyboards/images/tmp. gcyBAl37' failed: 'Invalid argume
Matlab function foundation (directly abandon version)
Design of reverse five times voltage amplifier circuit
Can the encrypted JS code and variable name be cracked and restored?
Realizing sequence annotation with transformers
Golang JSON unsupported value: Nan processing
Intra class data member initialization of static const and static constexpr
Parameter understanding of quad dataloader in yolov5
Idea automatically sets author information and date
nn. Modulelist and nn Sequential
CodeBlocks integrated Objective-C development