delimiter 常应用在存储过程 函数中,默认情况下,delimiter “;” 用于向 MySQL 提交查询语句。在存储过程中每个 SQL 语句的结尾都有个 “;”,如果这时候,每逢 “;” 就向 MySQL 提交的话,那么执行到";“就结束了。DELIMITER 先定好结束符为”$$", 按块去执行语句,然后最后再定义回";"。
示例:
DELIMITER $$
drop procedure if exists AddColumnUnlessExists $$
create procedure AddColumnUnlessExists(
IN dbName VARCHAR(50),
IN tableName VARCHAR(50),
IN fieldName VARCHAR(50),
IN fieldDef VARCHAR(50)
)
begin
IF NOT EXISTS (
SELECT * FROM information_schema.COLUMNS
WHERE column_name=fieldName
and table_name=tableName
and table_schema=dbName
)
THEN
set @str=CONCAT('ALTER TABLE ',dbName,'.',tableName,' ADD COLUMN ',fieldName,fieldDef);
prepare stmt from @str;
execute stmt;
END IF;
end; $$
DELIMITER ;