2

基本上我对这个论点一无所知,我在网上找到了一个代码并将其应用于我的代码:

DROP PROCEDURE IF EXISTS schema_change;  
delimiter ';;'  
CREATE PROCEDURE schema_change() BEGIN
/*Delete Columns*/
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'ticket_id')
    THEN
        ALTER TABLE razorphyn_support_uploaded_file DROP COLUMN `ticket_id`;
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_list_tickets' AND column_name = 'enc_id')
    THEN
        ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`,DROP INDEX `department_id`,DROP INDEX `operator_id`,DROP INDEX `user_id`,DROP INDEX `ticket_status`;
        ALTER TABLE razorphyn_support_list_tickets DROP COLUMN `enc_id`;
        ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`id`,`department_id`,`operator_id`,`user_id`,`ticket_status`);
END IF;

/*Rename Columns*/
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'num_id') 
    THEN(
        ALTER TABLE razorphyn_support_uploaded_file CHANGE num_id tk_id BIGINT(15) UNSIGNED NOT NULL;
    )
END IF;

IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_operator_rate' AND column_name = 'enc_id') 
    THEN
        ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`,DROP INDEX `enc_id`,DROP INDEX `rate`;
        ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`;

        ALTER TABLE razorphyn_support_operator_rate CHANGE enc_id tk_id BIGINT(15) UNSIGNED NOT NULL;

        ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`,`tk_id`,`rate`);
        ALTER TABLE razorphyn_support_list_tickets ADD UNIQUE KEY(`tk_id`);
END IF;

IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_flag_tickets' AND column_name = 'enc_id')
    THEN
        ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`,DROP INDEX `enc_id`,DROP INDEX `usr_id`;

        ALTER TABLE razorphyn_support_flag_tickets CHANGE enc_id tk_id BIGINT(15) UNSIGNED NOT NULL;

        ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`,`tk_id`,`usr_id`);
END IF;

ALTER TABLE razorphyn_support_list_messages ALTER COLUMN MODIFY `ticket_id` BIGINT(15) UNSIGNED NOT NULL;
END;;  

delimiter ';'  
CALL schema_change();  

DROP PROCEDURE IF EXISTS schema_change;  

但它抛出一个错误:

#1357 - Can't drop or alter a PROCEDURE from within another stored routine 

我不明白出了什么问题......
编辑
这些是表格:

CREATE TABLE IF NOT EXISTS `razorphyn_support_list_tickets` (
`id`                BIGINT(15)  UNSIGNED        NOT NULL    AUTO_INCREMENT,
`ref_id`            VARCHAR(18),
`department_id`     BIGINT(11)  UNSIGNED        NOT NULL,
`operator_id`       BIGINT(11)  UNSIGNED        NOT NULL DEFAULT 0,
`user_id`           BIGINT(11)  UNSIGNED        NOT NULL,
`title`             VARCHAR(255)                NOT NULL,
`priority`          INT(2)      UNSIGNED        NOT NULL,
`website`           VARCHAR(200)                NOT NULL,
`contype`           ENUM('0','1','2','3','4','5')   NOT NULL    DEFAULT '0',
`ftp_user`          VARCHAR(60)                 NOT NULL,
`ftp_password`      VARCHAR(60)                 NOT NULL,
`created_time`      DATETIME                    NOT NULL,
`last_reply`        DATETIME                    NOT NULL,
`ticket_status`     ENUM('0','1','2','3')       NOT NULL    DEFAULT '2',
`operator_rate`     DECIMAL(4,2)                UNSIGNED,
PRIMARY KEY (`id`),
UNIQUE KEY (`user_id`,`title`),
INDEX (`id`,`department_id`,`operator_id`,`user_id`,`ticket_status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;

CREATE TABLE IF NOT EXISTS `razorphyn_support_list_messages` (
`id`                BIGINT(18)  UNSIGNED        NOT NULL    AUTO_INCREMENT,
`user_id`           BIGINT(11)  UNSIGNED        NOT NULL,
`message`           TEXT                        NOT NULL,
`attachment`        ENUM('0','1')               NOT NULL    DEFAULT '0',
`ticket_id`         BIGINT(15)  UNSIGNED        NOT NULL,
`ip_address`        VARCHAR(20)                 NOT NULL,
`created_time`      DATETIME                    NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;

CREATE TABLE IF NOT EXISTS `razorphyn_support_uploaded_file`(
`id`                BIGINT(15)  UNSIGNED        NOT NULL AUTO_INCREMENT,
`name`              VARCHAR(50)                 NOT NULL,
`uploader`          BIGINT(11)  UNSIGNED        NOT NULL, 
`tk_id`             BIGINT(15)  UNSIGNED        NOT NULL,
`ticket_id`         CHAR(87),
`message_id`        BIGINT(18)  UNSIGNED        NOT NULL, 
`upload_date`       DATETIME                    NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`message_id`,`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10;

CREATE TABLE IF NOT EXISTS `razorphyn_support_flag_tickets`(
`id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
`ref_id`            VARCHAR(18)                     NOT NULL,
`tk_id`             BIGINT(15)      UNSIGNED        NOT NULL,
`usr_id`            BIGINT(15)      UNSIGNED        NOT NULL,
`side`              VARCHAR(20)                     NOT NULL,
`reason`            VARCHAR(200)                    NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`ref_id`,`tk_id`,`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;


CREATE TABLE IF NOT EXISTS `razorphyn_support_operator_rate`(
`id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
`ref_id`            VARCHAR(18)                     NOT NULL,
`tk_id`             BIGINT(15)      UNSIGNED        NOT NULL,
`usr_id`            BIGINT(15)      UNSIGNED        NOT NULL,
`rate`              DECIMAL(4,2)    UNSIGNED        NOT NULL    DEFAULT 0,
`note`              VARCHAR(200)                    ,
PRIMARY KEY (`id`),
UNIQUE KEY(`tk_id`),
INDEX (`ref_id`,`tk_id`,`rate`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;

这是整个文件:

    CREATE TABLE IF NOT EXISTS `razorphyn_support_users` (
    `id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
    `name`              VARCHAR(50)                     NOT NULL,
    `mail`              VARCHAR(50)                     NOT NULL,
    `password`          VARCHAR(200)                    NOT NULL,
    `reg_key`           VARCHAR(260)                    ,
    `tmp_password`      VARCHAR(87)                     ,
    `ip_address`        VARCHAR(50)                     NOT NULL,
    `status`            ENUM('0','1','2','3','4')       NOT NULL    DEFAULT '3',
    `holiday`           ENUM('0','1')                   NOT NULL    DEFAULT '0',
    `mail_alert`        ENUM('no','yes')                NOT NULL    DEFAULT 'yes',
    `assigned_tickets`  INT(5)          UNSIGNED        NOT NULL    DEFAULT 0,
    `solved_tickets`    BIGINT(11)      UNSIGNED        NOT NULL    DEFAULT 0,
    `number_rating`     BIGINT(6)       UNSIGNED        NOT NULL    DEFAULT 0,
    `rating`            DECIMAL(4,2)    UNSIGNED        NOT NULL    DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`mail`),
    INDEX (`name`,`mail`,`status`,`holiday`,`assigned_tickets`,`solved_tickets`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=55;

INSERT IGNORE INTO `razorphyn_support_users` (`id`, `name`, `mail`, `password`, `reg_key`, `tmp_password`, `ip_address`, `status`, `holiday`, `mail_alert`, `assigned_tickets`, `solved_tickets`, `number_rating`, `rating`) VALUES
(54, 'Admin', 'admin@admin.com', 'd16a2f5a824df504eafae57bae3ed217c5be8ffc15d8c576b536910e19b30ca27a3abe8f42e01222ec15e5f81a471f79428dbb940106e279d9cf45e50379c81e', NULL , NULL, '127.0.0.1', '2', '0', 'yes', 0, 0, 0, 0.00);

CREATE TABLE IF NOT EXISTS `razorphyn_support_list_tickets` (
    `id`                BIGINT(15)  UNSIGNED        NOT NULL    AUTO_INCREMENT,
    `ref_id`            VARCHAR(18),
    `department_id`     BIGINT(11)  UNSIGNED        NOT NULL,
    `operator_id`       BIGINT(11)  UNSIGNED        NOT NULL DEFAULT 0,
    `user_id`           BIGINT(11)  UNSIGNED        NOT NULL,
    `title`             VARCHAR(255)                NOT NULL,
    `priority`          INT(2)      UNSIGNED        NOT NULL,
    `website`           VARCHAR(200)                NOT NULL,
    `contype`           ENUM('0','1','2','3','4','5')   NOT NULL    DEFAULT '0',
    `ftp_user`          VARCHAR(60)                 NOT NULL,
    `ftp_password`      VARCHAR(60)                 NOT NULL,
    `created_time`      DATETIME                    NOT NULL,
    `last_reply`        DATETIME                    NOT NULL,
    `ticket_status`     ENUM('0','1','2','3')       NOT NULL    DEFAULT '2',
    `operator_rate`     DECIMAL(4,2)                UNSIGNED,
    PRIMARY KEY (`id`),
    UNIQUE KEY (`user_id`,`title`),
    INDEX (`id`,`department_id`,`operator_id`,`user_id`,`ticket_status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;

CREATE TABLE IF NOT EXISTS `razorphyn_support_list_messages` (
    `id`                BIGINT(18)  UNSIGNED        NOT NULL    AUTO_INCREMENT,
    `user_id`           BIGINT(11)  UNSIGNED        NOT NULL,
    `message`           TEXT                        NOT NULL,
    `attachment`        ENUM('0','1')               NOT NULL    DEFAULT '0',
    `ticket_id`         BIGINT(15)  UNSIGNED        NOT NULL,
    `ip_address`        VARCHAR(20)                 NOT NULL,
    `created_time`      DATETIME                    NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;

CREATE TABLE IF NOT EXISTS `razorphyn_support_uploaded_file`(
    `id`                BIGINT(15)  UNSIGNED        NOT NULL AUTO_INCREMENT,
    `name`              VARCHAR(50)                 NOT NULL,
    `uploader`          BIGINT(11)  UNSIGNED        NOT NULL, 
    `tk_id`             BIGINT(15)  UNSIGNED        NOT NULL,
    `ticket_id`         CHAR(87),
    `message_id`        BIGINT(18)  UNSIGNED        NOT NULL, 
    `upload_date`       DATETIME                    NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`message_id`,`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10;

CREATE TABLE IF NOT EXISTS `razorphyn_support_flag_tickets`(
    `id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
    `ref_id`            VARCHAR(18)                     NOT NULL,
    `tk_id`             BIGINT(15)      UNSIGNED        NOT NULL,
    `usr_id`            BIGINT(15)      UNSIGNED        NOT NULL,
    `side`              VARCHAR(20)                     NOT NULL,
    `reason`            VARCHAR(200)                    NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`ref_id`,`tk_id`,`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;

CREATE TABLE IF NOT EXISTS `razorphyn_support_departments` (
    `id`                BIGINT(11)  UNSIGNED    NOT NULL    AUTO_INCREMENT,
    `department_name`   VARCHAR(70)             NOT NULL,
    `active`            ENUM('0','1')           NOT NULL    DEFAULT '1',
    `public_view`       ENUM('0','1')           NOT NULL    DEFAULT '1',
    PRIMARY KEY (`id`),
    UNIQUE KEY (`department_name`),
    INDEX (`id`,`department_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;

CREATE TABLE IF NOT EXISTS `razorphyn_support_user_departments` (
    `id`                BIGINT(11)  UNSIGNED    NOT NULL    AUTO_INCREMENT,
    `department_id`     BIGINT(11)  UNSIGNED    NOT NULL,
    `department_name`   VARCHAR(70)             NOT NULL,
    `user_id`           BIGINT(11)  UNSIGNED    NOT NULL,
    `holiday`           ENUM('0','1')           NOT NULL    DEFAULT '0',
    PRIMARY KEY (`id`),
    UNIQUE KEY(`department_name`,`user_id`),
    INDEX(`department_id`,`department_name`,`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;

CREATE TABLE IF NOT EXISTS `razorphyn_support_operator_rate`(
    `id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
    `ref_id`            VARCHAR(18)                     NOT NULL,
    `tk_id`             BIGINT(15)      UNSIGNED        NOT NULL,
    `usr_id`            BIGINT(15)      UNSIGNED        NOT NULL,
    `rate`              DECIMAL(4,2)    UNSIGNED        NOT NULL    DEFAULT 0,
    `note`              VARCHAR(200)                    ,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`tk_id`),
    INDEX (`ref_id`,`tk_id`,`rate`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;

CREATE TABLE IF NOT EXISTS `razorphyn_support_faq` (
    `id`                INT(5)          UNSIGNED        NOT NULL AUTO_INCREMENT,
    `question`          VARCHAR(200)                    NOT NULL,
    `answer`            VARCHAR(2000)                   NOT NULL,
    `position`          INT(4)          UNSIGNED        NOT NULL    DEFAULT 0,
    `active`            ENUM('0','1')                   NOT NULL    DEFAULT '1',
    `num_rate`          INT(8)          UNSIGNED        NOT NULL    DEFAULT 0,
    `rate`              DECIMAL(4,2)    UNSIGNED        NOT NULL    DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`question`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;

CREATE TABLE IF NOT EXISTS `razorphyn_support_faq_rate`(
    `id`                BIGINT(15)      UNSIGNED        NOT NULL AUTO_INCREMENT,
    `faq_id`            INT(5)          UNSIGNED        NOT NULL,
    `usr_id`            BIGINT(15)      UNSIGNED        NOT NULL,
    `rate`              DECIMAL(4,2)    UNSIGNED        NOT NULL    DEFAULT 0,
    `updated`           ENUM('0','1')                   NOT NULL    DEFAULT '0',
    `note`              VARCHAR(200)                    ,
    PRIMARY KEY (`id`),
    UNIQUE KEY(`faq_id`,`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;


DROP PROCEDURE IF EXISTS schema_change;  
delimiter ';;'  
CREATE PROCEDURE schema_change() BEGIN
    /*Delete Columns*/
    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'ticket_id')
        THEN
            ALTER TABLE razorphyn_support_uploaded_file DROP COLUMN `ticket_id`;
    END IF;
    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_list_tickets' AND column_name = 'enc_id')
        THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`,DROP INDEX `department_id`,DROP INDEX `operator_id`,DROP INDEX `user_id`,DROP INDEX `ticket_status`;
            ALTER TABLE razorphyn_support_list_tickets DROP COLUMN `enc_id`;
            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`id`,`department_id`,`operator_id`,`user_id`,`ticket_status`);
    END IF;

    /*Rename Columns*/
    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'num_id') 
        THEN(
            ALTER TABLE razorphyn_support_uploaded_file CHANGE num_id tk_id BIGINT(15) UNSIGNED NOT NULL;
        )
    END IF;

    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_operator_rate' AND column_name = 'enc_id') 
        THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`,DROP INDEX `enc_id`,DROP INDEX `rate`;
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`;

            ALTER TABLE razorphyn_support_operator_rate CHANGE enc_id tk_id BIGINT(15) UNSIGNED NOT NULL;

            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`,`tk_id`,`rate`);
            ALTER TABLE razorphyn_support_list_tickets ADD UNIQUE KEY(`tk_id`);
    END IF;

    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_flag_tickets' AND column_name = 'enc_id')
        THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`,DROP INDEX `enc_id`,DROP INDEX `usr_id`;

            ALTER TABLE razorphyn_support_flag_tickets CHANGE enc_id tk_id BIGINT(15) UNSIGNED NOT NULL;

            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`,`tk_id`,`usr_id`);
    END IF;
    ALTER TABLE razorphyn_support_list_messages ALTER COLUMN MODIFY `ticket_id` BIGINT(15) UNSIGNED NOT NULL;
END;;  

delimiter ';'  
CALL schema_change();  

DROP PROCEDURE IF EXISTS schema_change;  
4

1 回答 1

0

下面的存储过程运行顺利。

调整:

DELIMITER ';;'渐渐地DELIMITER ;;_DELIMITER ';'DELIMITER ;

DROP PROCEDURE IF EXISTS `schema_change`;

DELIMITER ;;

CREATE PROCEDURE `schema_change`()
BEGIN
    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'ticket_id') THEN
            ALTER TABLE razorphyn_support_uploaded_file DROP COLUMN `ticket_id`;
    END IF;

    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_list_tickets' AND column_name = 'enc_id') THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`, DROP INDEX `department_id`, DROP INDEX `operator_id`, DROP INDEX `user_id`, DROP INDEX `ticket_status`;
            ALTER TABLE razorphyn_support_list_tickets DROP COLUMN `enc_id`;
            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`id`, `department_id`, `operator_id`, `user_id`, `ticket_status`);
    END IF;

    /*Rename Columns*/
    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_uploaded_file' AND column_name = 'num_id') THEN
            ALTER TABLE razorphyn_support_uploaded_file CHANGE `num_id` `tk_id` BIGINT(15) UNSIGNED NOT NULL;
    END IF;

    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_operator_rate' AND column_name = 'enc_id') THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`, DROP INDEX `enc_id`, DROP INDEX `rate`;
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `enc_id`;
            ALTER TABLE razorphyn_support_operator_rate CHANGE `enc_id` `tk_id` BIGINT(15) UNSIGNED NOT NULL;
            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`, `tk_id`, `rate`);
            ALTER TABLE razorphyn_support_list_tickets ADD UNIQUE KEY(`tk_id`);
    END IF;

    IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'razorphyn_support_flag_tickets' AND column_name = 'enc_id') THEN
            ALTER TABLE razorphyn_support_list_tickets DROP INDEX `ref_id`, DROP INDEX `enc_id`, DROP INDEX `usr_id`;
            ALTER TABLE razorphyn_support_flag_tickets CHANGE `enc_id` `tk_id` BIGINT(15) UNSIGNED NOT NULL;
            ALTER TABLE razorphyn_support_list_tickets ADD INDEX (`ref_id`, `tk_id`, `usr_id`);
    END IF;

    ALTER TABLE razorphyn_support_list_messages MODIFY COLUMN `ticket_id` BIGINT(15) UNSIGNED NOT NULL;
END;;

DELIMITER ;

CALL `schema_change`();
DROP PROCEDURE IF EXISTS `schema_change`;
于 2013-10-16T15:35:48.453 回答