1

我正在为 Mac OSX 编写安装脚本,并使用 shell 脚本来创建数据库和程序使用所需的所有表。我面临的问题是创建我们需要的程序。我尝试了多种实现方式,想知道是否有人知道一个例子或者是否有可能。以下是我所拥有的程序。我将不胜感激任何帮助。

步骤:

DELIMITER //
CREATE PROCEDURE Hours_Procedure()
BEGIN
DECLARE avg_usage FLOAT;
DECLARE max_time TIMESTAMP;
DECLARE counter INTEGER;
DECLARE NumOfNodes INTEGER;
DECLARE Num_Hour_Records INTEGER;

SET counter = 0;
SELECT MAX(Node_Num) INTO NumOfNodes FROM Minutes;

loop1 : LOOP SET avg_usage = 0.0;
 SET counter = counter + 1;
 IF counter = (NumOfNodes + 1) THEN LEAVE loop1;
 ELSE SELECT AVG(x.Power_Usage), MAX(x.Record_Time) INTO avg_usage, max_time FROM(
  SELECT M.Power_Usage, M.Record_Time FROM Minutes M
  WHERE M.Node_Num = counter ORDER BY RID DESC LIMIT 60) x;
  INSERT INTO Hours(Node_Num, Record_Time, Power_Usage) VALUES (counter, max_time, avg_usage);
  DELETE FROM Minutes WHERE Node_Num = counter ORDER BY RID ASC LIMIT 60;
 END IF;
END LOOP loop1;

DROP INDEX Hours_Index ON Hours;
CREATE INDEX Hours_Index ON Hours(RID, Node_Num);
END// DELIMITER ;

我尝试了什么:

$mysql -u $_adminuser -h $_host -Bse "USE $_hostdb; $createProc;"

其中 $createProc 是创建过程的代码。

4

1 回答 1

3

将您的过程存储在文件procedure.sql中,请尝试以下操作。它通过在命令行上指定数据库来消除该USE语句:

$mysql --batch --silent -u $_adminuser -h $_host $_hostdb < procedure.sql
于 2011-04-25T19:41:00.363 回答