But did you know that you can also use JSON with MySQL Stored Procedures - making them very flexible?
Less talk, more code:
(Data was used from the UK Land Registry that I worked on)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delimiter $$ | |
drop procedure if exists county_summary$$ | |
create procedure county_summary(query JSON) | |
BEGIN | |
DECLARE i_county varchar(255) default null; | |
DECLARE i_year int default null; | |
DECLARE i_month int default null; | |
set i_county = JSON_UNQUOTE(JSON_EXTRACT(query,'$.county')); | |
set i_year = JSON_EXTRACT(query,'$.year'); | |
set i_month = JSON_EXTRACT(query,'$.month'); | |
if (i_county is not null) && (i_year is not null) && (i_month is not null) then | |
select county, year,month, sum(total_price) as total_price, sum(houses_sold) as houses_sold | |
from summary | |
where county = i_county and year = i_year and month=i_month | |
group by county; | |
elseif (i_county is not null) && (i_year is not null) then | |
select county, year, sum(total_price) as total_price, sum(houses_sold) as houses_sold | |
from summary | |
where year = i_year | |
and county = i_county | |
group by county, year; | |
elseif (i_year is not null) && (i_month is not null) then | |
select county, year, month, sum(total_price) as total_price, sum(houses_sold) as houses_sold | |
from summary | |
where year = i_year and month = i_month | |
group by county; | |
elseif (i_year is not null) then | |
select county, year, sum(total_price) as total_price, sum(houses_sold) as houses_sold | |
from summary | |
where year = i_year | |
group by county; | |
else | |
select null as 'no input'; | |
end if; | |
END $$ | |
delimiter ; | |
-- Testing | |
call county_summary('{"year": 2010}'); | |
call county_summary('{"county":"YORK","year": 2010,"month":12}'); | |
call county_summary('{"county":"YORK","year": 2010}'); | |
call county_summary('{"year": 2010,"month":12}'); |
This method will be useful to work with node.js and the native mysql driver, just stringify the object to json, I will try
ReplyDeleteUsing the JSON datatype for stored procedure params effectively gives us SPs with optional params. This takes stored procedures to a new level by making them a lot more flexible.
ReplyDeletenice
ReplyDeleteI'm getting a s yntax error when I use:
ReplyDeleteCREATE PROCEDURE procedure_name(query JSON)
It says that the JSON identifier is unexpected.
You using mysql 5.7?
DeleteYes, maybe MySQL Workbench has an issue?
Deletedear jonathan , I am very much new to mysql and json and encountering a prob .. requesting you please bear with me ..
ReplyDeleteactually the thing is ... I have to read a table and stored in JSON array inside the stored procedure in mysql ( how we can achieve this) .. the intention behind this .. i have to generate a JSON file . please help me
There are some MySQL JSON functions in 5.7 to read JSON arrays, but bare in mind that they are limited and your best course of action is to read those using a programming language.
ReplyDeleteI am however, happy to help if you have a specific issue with some details.