21 Dec 2016

JSON and MySQL Stored Procedures

You probably heard that MySQL 5.7 supports JSON.
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)

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}');
view raw json.sql hosted with ❤ by GitHub

8 comments:

  1. This method will be useful to work with node.js and the native mysql driver, just stringify the object to json, I will try

    ReplyDelete
  2. Using 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.

    ReplyDelete
  3. I'm getting a s yntax error when I use:
    CREATE PROCEDURE procedure_name(query JSON)

    It says that the JSON identifier is unexpected.

    ReplyDelete
  4. dear jonathan , I am very much new to mysql and json and encountering a prob .. requesting you please bear with me ..

    actually 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

    ReplyDelete
  5. 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.
    I am however, happy to help if you have a specific issue with some details.

    ReplyDelete