当我想声明此过程时,它不会声明此代码不报告任何报告
我不知道那是问题所在
我想在solr索引中使用此过程,但此过程不会执行
DELIMITER $$
create procedure getAllStuff()
begin
declare category_id int(10);
declare category_name varchar default NULL;
declare stateCommands varchar(255) default NULL;
declare leafCats INT(10) default NULL;
declare tableName varchar(255) default NULL;
declare finished int(10) default 0;
declare leafCats_cursor CURSOR FOR select id,name from category where rgt=lft+1;
declare CONTINUE handler FOR NOT FOUND set finished=1;
create temporary table IF NOT EXISTS leafCats (
id int null primary key auto_increment,
category_id int,
tableName varchar(255)
);
open leafCats_cursor;
set_leafCats: LOOP
fetch leafCats_cursor into category_id,category_name;
if finished =1 then
leave set_leafCats
endif
set tableName=replace(catgeory_name,' ','_');
set tableName = concat('stuff_',tableName);
insert into leafCats values (NULL,category_id,tableName);
end loop set_leafCats;
close leafCats_cursor;
declare cats_cursor CURSOR FOR select category_id,category_name from leafCats;
open cats_cursor;
get_cats: LOOP
fetch cats_cursor into category_id,category_name;
if finished =1 then
leave set_leafCats;
endif;
if stateCommands != NULL then
set stateCommands=concat(sql,'select t.id as id,t.name,t.overall,c.id as
category_id from '.tableName .' t join category c where c.id=' . category_id);
else
set stateCommands=concat(sql,'union all select t.id asid,t.name,t.overall,c.id as
category_id from '.tableName .' t join category c where c.id=' . category_id);
end if;
end loop get_cats;
close cats_cursor;
PREPARE s1 FROM stateCommands;
EXECUTE s1;
DEALLOCATE PREPARE s1;
end @@
DELIMITER ;
当我将分隔符改为@@这个错误apear
错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'default NULL附近使用正确的语法;声明stateCommands varchar(255)默认为NULL;声明leafCats'在第4行mysql> DELIMITER;
1 回答
我评论了几点要考虑:
IS NOT NULL :测试值是否为NULL . 12.3.2. Comparison Functions and Operators .
preparable_stmt 是字符串文字或包含SQL语句文本的用户变量 . 13.5.1. PREPARE Syntax .
游标声明必须出现在处理程序声明之前以及变量和条件声明之后 . 13.6.6. Cursors .
我还添加了一个我认为您可能会觉得有用的示例:
你将得到如下的声明,它将被执行: