存储过程变量在MySQL中没有返回预期值?

时间:2011-08-13 23:53:53

标签: mysql sql stored-procedures

我正在调试此代码

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');

@t在结果集中返回'a',但@ cnt,@ base和@offset不会。我无法解释自己的原因。你能给我一些帮助吗?

2 个答案:

答案 0 :(得分:1)

在存储过程结束时尝试单个SELECT:

  SELECT 
       t as 'param', 
       @cnt as 'count', 
       @base as 'base', 
       @offset as 'offset', 
       @query as 'query';

答案 1 :(得分:0)

问题似乎出现在SELECT中。 '='运算符是比较或类似的东西,而在这种情况下实现所需的行为':='应该被使用。

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

此更改使整个代码正常工作。