我正在尝试实现一个simple counter,它在SQL查询的几个语句中作为参数传递(见下文)。我有以下测试环境
CREATE TABLE `test`.`TableA` (
`ColumnA` INT( 3 ) NOT NULL ,
`ColumnB` INT( 5 ) NOT NULL
) ENGINE = MYISAM ;
ALTER TABLE `TableA` ADD INDEX ( `ColumnA` )
INSERT INTO `TableA` (`ColumnA`, `ColumnB`) VALUES
(2, 4654), (2, 223), (5, 12), (4, 32), (3, 23), (5, 21), (1, 2), (2, 2), (4, 2), (5, 1);
我想为每个计数器迭代编写一个带编号的输出文件(而AFAIK的一种方法就是使用存储过程)
CREATE PROCEDURE dowhile()
BEGIN
DECLARE CONT INT DEFAULT 5
WHILE CONT > 1 DO
BEGIN
SELECT TableA.ColumnB
INTO OUTFILE CONCAT('OUT', CONT)
FROM TableA
WHERE TableA.ColumnA = CONT
SET CONT = CONT - 1
END
END WHILE
END;
答案 0 :(得分:0)
看起来有两个问题:
通过大量的谷歌搜索,我得到了以下解决方案,该解决方案适用于我的MySql 5.1.51:
delimiter //
CREATE PROCEDURE doWhile()
BEGIN
DECLARE CONT INT DEFAULT 5;
DECLARE filename VARCHAR(32);
WHILE CONT > 1 DO
SET @myvar = CONCAT('SELECT TableA.ColumnB INTO OUTFILE', "'", CONCAT('OUT', CONT), "'", 'FROM TableA WHERE TableA.ColumnA = ', CONT );
PREPARE stmt1 FROM @myvar;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET CONT = CONT - 1;
END WHILE;
END