MySQL Shell Scripting创建过程

时间:2021-10-28 16:30:10

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 the 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.

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

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.

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

1 个解决方案

#1


3  

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:

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

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

#1


3  

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:

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

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