1

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?

2 Answers 2

1

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';
Sign up to request clarification or add additional context in comments.

1 Comment

@munch: where is @cnt declared? I see you are using in inside a dynamic sql query, bu have you declared it before? If not - try out to declare before query
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.