use function in stored procedure in mysql -
i have created stored procedure in mysql:
create definer=`root`@`localhost` procedure `get_publicationsbydate`(in _dates date, in _datef date) begin select concat(extract(year filed_date), " q", extract(quarter filed_date)) date, count(concat(extract(year filed_date), " q", extract(quarter filed_date))) count applications filed_date between _dates , _datef group concat(extract(year filed_date), " q", extract(quarter filed_date)) order concat(extract(year filed_date), " q", extract(quarter filed_date)); end
i want replace long code
concat(extract(year filed_date), " q", extract(quarter filed_date))
of function call (еxample in pseudo c ++ convenience):
char* func(char* date) { return concat(extract(year date), " q", extract(quarter date)) }
because these long expressions may vary depending on external parameter (pseudo code):
declare f function; if (_type = 0) set f = func1; else set f = func2; end if; select f(filed_date), count(f(filed_date))...
how in mysql create it?
create function decide problem.
it pity function of external
create definer=`root`@`localhost` function `prepare_scalecondition`(_scale varchar(16), _date date) returns varchar(64) charset utf8 begin declare _scalecondition varchar(128) default extract(year _date); case _scale when 'quarter' set _scalecondition = concat(extract(year _date), ' q', extract(quarter _date)); when 'year' set _scalecondition = extract(year _date); when 'month' set _scalecondition = concat(extract(year _date), ' m', extract(month _date)); when 'week' set _scalecondition = concat(extract(year _date), ' w', extract(week _date)); end case; return _scalecondition; end create definer=`root`@`localhost` procedure `get_publicationsbydate`(in _dates date, in _datef date) begin select prepare_scalecondition(filed_date) date, count(prepare_scalecondition(filed_date)) count applications filed_date between _dates , _datef group prepare_scalecondition(filed_date) order prepare_scalecondition(filed_date); end
Comments
Post a Comment