开发者

MySQL Shell Scripting to create Procedures

I'm working on an install script for Mac OSX and am using a shell script to create a database and all the tables I need for the program to use. The problem that I am facing is th开发者_开发问答e creating of the procedure we need. I have tried multiple way of implementing and wondered if anyone knows of an example or if it is possible. Below is what I have as far as the procedure goes. I would appreciate any help.

The Procedure:

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 ;

What I tried:

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

Where $createProc is the code to create the procedure.


With your procedure stored in the file procedure.sql, try the following. It eliminates the USE statement by specifying the database on the command line:

$mysql --batch --silent -u $_adminuser -h $_host $_hostdb < procedure.sql
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜