在nodejs中创建一个新的MySQL存储过程

时间:2017-07-12 12:49:37

标签: javascript mysql sql node.js

我使用mysql库与数据库进行通信。它支持调用存储过程。有人知道这个库或其他npm包是否可以创建存储过程?这似乎无法做到。

额外信息

我本来想要做的是为我的数据库创建一个重置脚本,删除所有表和视图。如果我没有存储过程就能做到这一点,很好!目前,IF子句需要存储过程。

这就是我得到的:

DROP PROCEDURE IF EXISTS drop_all_tables;
DELIMITER $$
CREATE PROCEDURE drop_all_tables()
BEGIN
  SET FOREIGN_KEY_CHECKS = 0;
  SET @tables = NULL;
  SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
    FROM information_schema.tables 
    WHERE table_schema = 'my_database'
    AND table_type = 'BASE TABLE';
  IF @tables IS NOT NULL THEN BEGIN
    SET @tables = CONCAT('DROP TABLE ', @tables);
    PREPARE stmt FROM @tables;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
  END; END IF;
  SET FOREIGN_KEY_CHECKS = 1;
END $$
DELIMITER ;
CALL drop_all_tables();
DROP PROCEDURE IF EXISTS drop_all_tables;

DROP PROCEDURE IF EXISTS drop_all_views;
DELIMITER $$
CREATE PROCEDURE drop_all_views()
BEGIN
  SET FOREIGN_KEY_CHECKS = 0;
  SET @views = NULL;
  SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @views
    FROM information_schema.tables 
    WHERE table_schema = 'my_database'
    AND table_type = 'VIEW';
  IF @views IS NOT NULL THEN BEGIN
    SET @views = CONCAT('DROP VIEW ', @views);
    PREPARE stmt FROM @views;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
  END; END IF;
  SET FOREIGN_KEY_CHECKS = 1;
END $$
DELIMITER ;
CALL drop_all_views();
DROP PROCEDURE IF EXISTS drop_all_views;

0 个答案:

没有答案