mysql使用命令行执行存储过程
编写存储过程sql
以给brand表添加phone字段为例:
DROP PROCEDURE IF EXISTS UpdateColum;
CREATE PROCEDURE UpdateColum()
BEGIN
SELECT database() INTO @dbName;
SET @tablename = 'brand';
SET @column_name = 'phone';
SELECT
CONCAT(
'select count(*) into @colName from information_schema.columns where table_name = ''',
@tablename,
''' and column_name = ''',
@column_name,
''' and table_schema=''',
@dbName,
''';'
)
INTO
@ckstring;
PREPARE SQLStr1 FROM @ckstring;
EXECUTE SQLStr1;
DEALLOCATE PREPARE SQLStr1;
IF @colName = 0 THEN
ALTER TABLE
brand
ADD
phone varchar(255) NOT NULL DEFAULT '';
END IF;
END;
CALL UpdateColum();
DROP PROCEDURE IF EXISTS UpdateColum;
命令行方式执行存储过程
执行以下命令:
命令1: 选择操作的数据库
user dbname ;
命令2:修改行分割符
delimiter ;;
命令3:执行sql文件
source /root/xxx.sql
命令4:恢复行分割符
delimiter ;