postgresql 儲存過程處理json字串

2021-10-08 14:02:39 字數 1592 閱讀 6092

函式的引數傳入值為json格式的字串,通過遍歷,獲取某個字段值。之後進行處理。

下面的示例中,

p_data進行了賦值,陣列長度是2.

,

"hwf": 1,

"fqcy": 60,

"hgps": 1,

"etwd": 230720,

"uid": "fbc09a5ea974f60d4713ff9f",

"pro": "北京市",

"city": "北京市",

"dist": "海淀區",

"str": "建材城東路",

"lon": 116.3690191,

"lat": 40.0602989,

"radius": 50,

"desc": ""}],

"ret": "succ"

}

create or replace function "esys"."fntest123"()

returns "pg_catalog"."void" as $body$ begin-- routine body goes here...

declare

p_data varchar (4000);

ppp varchar(1000);

lll varchar(1000);

tempdata varchar;

rs record;

begin

for rs in (select "value" as single_value from json_array_elements_text(p_data::json)) loop

select t1.pwd::text into ppp from (select (rs.single_value) ::json #>> '' as pwd) as t1;

raise notice 'pwd is %',ppp;

select t1.pwd::text into ppp from (select (rs.single_value) ::json #>> '' as pwd) as t1;

raise notice 'wt is %',ppp;

select t1.pwd::text into lll from (select (rs.single_value) ::json #>> '' as pwd) as t1;

raise notice 'wt_lon %',lll;

end loop;

end;

return;

end $body$

language plpgsql volatile

cost 100

輸出結果如下

注意:  pwd is 123456

注意: wt is

注意: wt_lon 116.3685121

注意: pwd is 123456

注意: wt is

注意: wt_lon 116.3685121

procedure executed successfully

Postgresql儲存過程

pg的儲存過程與oracle的稍微有點不一樣,它的結構是這樣的 語法 create or replace function function name arg1,arg2.returns return type as body declare 變數宣告 begin 函式體end body langu...

postgresql之儲存過程

特性 儲存過程舉例 1 引數列表只包含函式輸入引數,不包含輸出引數 儲存過程定義 create or replace function add a integer,b numeric returns numeric as select a b language sql 呼叫方法 select add...

PostgreSQL儲存過程(函式)

create or replace function function name 引數逗號隔開 returns 返回值型別 as body declare 宣告變數 變數名 變數型別 變數值 例如 name char 20 su begin 函式體 包括dml語句 特別注意 如果有返回值,要省略最後...