用于创建过程的 MySQL Shell 脚本

发布于 2024-11-03 04:07:47 字数 1177 浏览 4 评论 0原文

我正在编写 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 是创建过程的代码。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-11-10 04:07:47

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文