Tuesday, 28 August 2018

The stored procedure variable does not return the expected value in MySQL?

I'm debugging this code

create procedure create_view ( IN t varchar(50))
BEGIN
  prepare stmt from 'select @cnt= count( weight ) from test where url = ?;';
  execute stmt using @t;
  set @base = @cnt /4;
  set @offset = @cnt / 2;
  set @query = concat('create or replace view view_by_url as select url, weight from test where url = ',@t,' order by weight limit  ',@base,' , ',@offset,' ;');
  select t as 'param';
  select cnt as 'count';
  select @base as 'base';
  select @offset as 'offset';
  select @query as 'query';
 -- prepare stmt from @query;
 -- execute stmt ;
END;
call create_view('a');

And @t returns 'a' in result set but @cnt, @base and @offset don't. And I can't explain myself why. Can you give me some help?

Try out single SELECT at the end of stored procedure:
  SELECT
       t as 'param',
       @cnt as 'count',
       @base as 'base',
       @offset as 'offset',
       @query as 'query';

0 comments:

Post a Comment