2013-12-09 26 views
0

當我要聲明此過程不聲明 此代碼不會報告任何報告
我不知道這是我想使用此過程中的問題
MySQL的 - 程序運行失敗不是錯誤

在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服務器版本相對應的手冊,以便在「默認NULL; declare stateCommands varchar(255)default NULL; 在第4行聲明瞭leafCats' mysql> DELIMITER;

+0

檢查語法錯誤。所有的DECLARE語句都必須在BEGIN..END子句的開頭。 – Devart

+0

就沒有錯誤 - > DELIMITER; - > DELIMITER; –

+0

存在語法錯誤。添加更多關於客戶的信息,顯示錯誤信息。 – Devart

回答

1

我評論幾點考慮:

我還補充一點,我覺得你可能會覺得有用的例子:

DELIMITER $$ 

DROP PROCEDURE IF EXISTS `getAllStuff`$$ 

CREATE PROCEDURE `getAllStuff`() 
BEGIN 
    DECLARE `finished` TINYINT(0) DEFAULT 0; 
    DECLARE `category_id` INT UNSIGNED; 
    DECLARE `category_name` VARCHAR(255); 
    DECLARE `leafCats_cursor` CURSOR FOR 
    SELECT `id`, `name` FROM `category` WHERE `rgt` = `lft` + 1; 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET `finished` = 1; 
    SET @`statecommands` := NULL; 
    OPEN `leafCats_cursor`; 
    `set_leafCats`: LOOP 
     FETCH `leafCats_cursor` INTO `category_id`, `category_name`; 
     IF `finished` = 1 THEN 
      LEAVE `set_leafCats`; 
     END IF; 
     SET @statecommands := CONCAT(IF(@statecommands IS NOT NULL, CONCAT(@statecommands, ' \nUNION ALL'), ''), ' 
      SELECT 
       `t`.`id` AS `id`, 
       `t`.`name`, 
       `t`.`overall`, 
       `c`.`id` AS `category_id` 
      FROM `', CONCAT('stuff_', REPLACE(`category_name`, ' ', '_'), '`'), ' `t` 
       JOIN `category` `c` WHERE `c`.`id` = ', `category_id`); 
    END LOOP `set_leafCats`; 
    CLOSE `leafCats_cursor`; 
    PREPARE `exec` FROM @`statecommands`; 
    EXECUTE `exec`; 
    DEALLOCATE PREPARE `exec`; 
END$$ 

DELIMITER ; 

你會得到像下面這樣,將被執行的語句:

SELECT 
    `t`.`id` AS `id`, 
    `t`.`name`, 
    `t`.`overall`, 
    `c`.`id` AS `category_id` 
FROM `stuff_category_1` `t` 
    JOIN `category` `c` WHERE `c`.`id` = 1 
UNION ALL 
SELECT 
    `t`.`id` AS `id`, 
    `t`.`name`, 
    `t`.`overall`, 
    `c`.`id` AS `category_id` 
FROM `stuff_category_2` `t` 
    JOIN `category` `c` WHERE `c`.`id` = 2 
UNION ALL 
SELECT 
    `t`.`id` AS `id`, 
    `t`.`name`, 
    `t`.`overall`, 
    `c`.`id` AS `category_id` 
FROM `stuff_category_3` `t` 
    JOIN `category` `c` WHERE `c`.`id` = 3 
UNION ALL 
SELECT 
    `t`.`id` AS `id`, 
    `t`.`name`, 
    `t`.`overall`, 
    `c`.`id` AS `category_id` 
FROM `stuff_category_4` `t` 
    JOIN `category` `c` WHERE `c`.`id` = 4; 
+0

非常感謝你,你救了我,謝謝 –

相關問題