程序运行时间

时间:2017-05-16 02:13:42

标签: mysql sql

DELIMITER $ 
DROP PROCEDURE IF EXISTS CREATE_BACKUP$
CREATE PROCEDURE CREATE_BACKUP()
BEGIN 
DECLARE BACK INT DEFAULT 0;
SELECT TABLE_NAME 
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'STUDENTDB'
;

    SHOW_LOOP:LOOP

        IF BACK = 1 
        THEN  
        LEAVE SHOW_LOOP;
        END IF;

        CREATE TABLE STUDENT_BACKUP 
        AS SELECT * FROM STUDENT;
        CREATE TABLE SCORE_BACKUP 
        AS SELECT * FROM SCORE;
        CREATE TABLE GRADE_EVENT_BACKUP 
        AS SELECT * FROM grade_event;
    END LOOP SHOW_LOOP;

END$
DELIMITER ;

嗨,当我运行此程序时,它会运行多次。所以当它运行时第二次出现“STUDENT_BACKUP表已经存在”的错误。我应该怎么做才能运行一次?

1 个答案:

答案 0 :(得分:1)

在MySQL中,您可以使用CREATE TABLE IF NOT EXIST...来避免错误发生。有关详细信息,请参阅CREATE TABLE syntax
要解决您对SQL Server的疑问,请使用INFORMATION_SCHEMA视图。类似的解决方案在existing topic中。