mysql儲存過程中遍歷json資料

2021-08-18 21:32:39 字數 2165 閱讀 8451

一、今天在開發中遇到mysql中遍歷json資料的需求,查了些資料,然後特此記錄,

二、在mysql5.7中是有乙個方法用來取json資料的,json_extract(json.val,key),詳細**如下

三、**如下:

create definer=`root`@`localhost` procedure `fun_sys_user_local_mobilereg`(puserid int,pip varchar(95),params varchar(5000),out prlt varchar(800))

begin

-- bs使用者移動版啟用--本地儲存過程

-- ,"usernolen":3}

declare vuserno,vusernotmp,vusernotmpkey varchar(200);

declare vusernolen int;

declare i int default 0;

set vuserno = params->>'$.userno';

set vusernolen = params->>'$.usernolen';

while icreate definer=`jovie`@`%` procedure `fun_api_uploadempattinfo`(puserid int,pip varchar(95),params varchar(5000),out prlt varchar(800))

begin

-- 考勤記錄處理

-- ,]}

declare vclockno,vcardno,vcardtime,voriginal,vkeytmp varchar(45);

declare vrecdata,rectmp varchar(5000);

declare vempid,i,vreclen int default 0;

declare vcarddate date;

set vrecdata = params->>'$.recdata';

set vreclen = json_length(vrecdata);

while i < vreclen do 

set vkeytmp = concat('$[',i,']'); 

select json_extract(vrecdata, vkeytmp) into rectmp;

set vclockno = json_extract(rectmp, '$.clockno');

set vclockno = replace(vclockno,'\"','');

set vcardno = json_extract(rectmp, '$.cardno');

set vcardno = replace(vcardno,'\"','');

set vcarddate = json_extract(rectmp, '$.carddate');

set vcarddate = replace(vcarddate,'\"','');

set vcardtime = json_extract(rectmp, '$.cardtime');

set vcardtime = replace(vcardtime,'\"','');

set vcarddate = cast(vcardtime as date);

set voriginal = concat(vclockno,'_',vcardno,'_',vcardtime);

select empid into vempid from kqcard where cardno = vcardno;   

set i = i + 1;

if not exists(select original from epcardoriginalrec where original = voriginal) then

insert into epcardrec (clockno,cardno,empid,carddate,cardtime,original)

values (vclockno,vcardno,vempid,vcarddate,vcardtime,voriginal);       

end if;        

end while;    

set prlt= concat('');

end

儲存過程中呼叫儲存過程

use northwind go 儲存過程1 功能 通過員工firstname inputempfirstname 獲得 員工id outid if exists select name from sysobjects where name p getempleeidbyname and type ...

oracle儲存過程中陣列申明和遍歷

create or replace procedure test as 申明陣列型別 type t test is table of varchar2 9 test t test t test a b c d e f g h j k l begin 遍歷陣列,將遍歷得到的資料插入到aa表中 for ...

Mysql的儲存過程中的異常

以前看到一篇mysql的儲存過程,覺得很簡單 要使用mysql的儲存過程,需要 1 mysql的版本在5.0以上,低版本的海不支援儲存過程 2 資料表應該是innodb的,其他格式的不支援事務 做乙個實驗 建立兩個表,在儲存過程中向兩個表插入資料,使向第一表的插入操作執行成功,向第二個表的操作執行失...