当前位置:网站首页>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;
边栏推荐
- Zlmediakit push pull flow test
- Idea update
- (2) Buzzer
- Record the problem yaml file contains Chinese message 'GBK' error
- opencv学习笔记三
- Use a switch to control the lighting and extinguishing of LEP lamp
- Is it safe to open an account in flush,
- 监听iPad键盘显示和隐藏事件
- Ltp-- extract time, person and place
- Mapping '/var/mobile/Library/Caches/com.apple.keyboards/images/tmp.gcyBAl37' failed: 'Invalid argume
猜你喜欢

【Unity Mirror】NetworkTeam的使用

Fabrication of modulation and demodulation circuit

FFmpeg音视频播放器实现

51 MCU project design: Based on 51 MCU clock perpetual calendar

1GHz active probe DIY

Stream analysis of hevc learning

Introduction of laser drive circuit

Use a switch to control the lighting and extinguishing of LEP lamp

SOC wireless charging scheme

SQL learning experience (II): question brushing record
随机推荐
h5 localStorage
WBC learning notes (I): manually push WBC formula
Swift code implements method calls
FFmpeg音视频播放器实现
nn. Modulelist and nn Sequential
Undefined symbols for architecture i386 is related to third-party compiled static libraries
KNN resolution
[resolved]setonnavigationitemselectedlistener() deprecated
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
鲸会务为活动现场提供数字化升级方案
2020-10-17
opencv学习笔记二
[postgraduate entrance examination] group planning exercises: memory
[已解决]setOnNavigationItemSelectedListener()被弃用
Ltp-- extract time, person and place
【Unity Mirror】NetworkTeam的使用
Use a switch to control the lighting and extinguishing of LEP lamp
Two ways to realize time format printing
GHUnit: Unit Testing Objective-C for the iPhone
2020-10-29