开发者

Stored procedure variable not returning 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';


The problem seems to be in the SELECT. '=' operator is comparison or something like that while to achieve desired behavior in this case ':=' should be used.

prepare stmt from 'select @cnt= count( weight ) from test where url = ?;';

This change makes the whole piece of code work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜