View Single Post
  #20 (permalink)  
Old 08-02-2007, 02:19 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Use on group_concat

Hi,

The problem is u cannot use the declared variable as in prepare statement command.

drop procedure if exists sp_test;
create procedure sp_test()
sp_test:
begin
declare var_query text;
declare var_id integer;
SET var_query:= 'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO var_Id FROM mytable';
SET @var_query := var_query;
prepare pre_query FROM @var_query;
execute pre_query;
select var_id;
end sp_test;

CALL sp_test();

Now again u will get an error Undeclared variable: var_Id;, indicates that the dynamic sql query can save the value in the global/session variable inside the single query, i.e
'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO @var_Id FROM mytable';

Can't be like this
'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO var_Id FROM mytable';

So here is the code,

Quote:
drop procedure if exists sp_test;
create procedure sp_test()
sp_test:
begin
declare var_query text;
declare var_id integer;
SET var_query:= 'SELECT CAST(GROUP_CONCAT(mytable_id)AS BINARY) INTO @var_Id FROM mytable';
SET @var_query := var_query;
prepare pre_query FROM @var_query;
execute pre_query;
select @var_id;
end sp_test;
Quote:
Output:
1,2,3
__________________
-Murali..
Reply With Quote