MySQL中是否有“创建或替换过程”的替代方法?


16

MySQL是否有版本的“创建或替换过程”?我似乎无法执行此操作,或者在重新编译之前编写该过程的删除脚本(如果存在),而不会得到存储过程存在的错误消息。

DELIMITER $$

-- would love to be able to drop procedure if exists db.sp_tmp_90days;  
-- or use "create or replace"

create procedure db.sp_tmp_90days()

BEGIN
drop table db.tmp_90days;

create table db.tmp_90days ( 
    user_name varchar(128), 
    first_name varchar(50), 
    last_name varchar(50), 
    system varchar(10), 
    last_login datetime 
);

alter table db.tmp_90days add index idx_user_name(user_name);
alter table db.tmp_90days add index idx_system(system);
alter table db.tmp_90days add index idx_last_login(last_login);

insert into db.tmp_90days (user_name, first_name, last_name, system, last_login)
    SELECT
        [...]
END $$

Answers:


20

这是删除它的语法(如果存在)

DROP PROCEDURE IF EXISTS db.sp_tmp_90days;

3
还可以提到,MariaDB是MySQL的“直接替代品”,CREATE OR REPLACE PROCEDURE自版本10.1起就支持该语法。
dbdemon
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.