【www.gdgbn.com--mysql教程】

mysql教程导入存储过程的方法
本文章提供二种关于mysql导入存储过程的方法与mysql存储过程导出方法,下面我们先来看看导出存储过程的实例,再看一款详细的导入存储过程方法。
导出mysql里面的函数或者存储过程

语法为:
[root@localhost bin]# mysqldump -uroot -p -hlocalhost -p3306 -n -d -t -r dbname > procedure_name.sql
参数说明:
-n:   --no-create-db
-d:   --no-data
-t:   --no-create-info
-r:   --routines dump stored routines (functions and procedures)

 


     mysqldump -hhostname -uusername -ppassword -ntd -r databasename > backupflie.sql

     mysqldump -hlocalhost -uroot -ntd -r hqgr > hqgr.sql

   其中的 -ntd 是表示导出存储过程;-r是表示导出函数


导入mysql存储过程

1 # mysqldump -u 数据库教程用户名 -p -n -t -d -r 数据库名 > 文件名

  其中,-d 表示--no-create-db, -n表示--no-data, -t表示--no-create-info, -r表示导出function和procedure。所以上述代码表示仅仅导出函数和存储过程,不导出表结构和数据。但是,这样导出的内容里,包含了trigger。再往mysql中导入时就会出问题,错误如下:

  error 1235 (42000) at line **: this version of mysql doesn"t yet support "multiple triggers with the same action time and event for one table"

  所以在导出时需要把trigger关闭。代码为

  1 # mysqldump -u 数据库用户名 -p -n -t -d -r --triggers=false 数据库名 > 文件名

  这样导入时,会出现新的问题:

  errorcode:1418

  this function has none of deterministic, nosql, or reads sql data inits declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

  解决方法是,在/etc/my.cnf中找到[mysqld],在它下面添加这样一行:

  1 log-bin-trust-function-creators=1

 

导入存储过程方法

delimiter $$

create procedure `pms_authuser`(in account varchar(255), in vorgid varchar(255))
    not deterministic
    contains sql
    sql security definer
    comment ""
begin
   declare cnt integer;
   declare pmsc varchar(100);
   select @pmsc:=orgid from organization where pmscode=vorgid;
   select @cnt:=count(*) from userinfo as userinfo where userinfo.username=account and userinfo.orgid =@pmsc;
   if @cnt>0
   then
   select userinfo.*,org.orgid as orgdepth from userinfo as userinfo ,organization as org where userinfo.username=account and userinfo.orgid =@pmsc and userinfo.orgid=org.orgid;
   else
   select userinfo.*,org.orgdepth from userinfo userinfo,organization org where username=account and userinfo.orgid in(select orgid from organization where orgdepth like concat("%",@pmsc,"%"));
   end if;
end $$
delimiter;

然后mysql本机运行命令导入数据库中的存储过程
mysql -uroot crs2 < file.sql


如果非本机mysql工具导入的存储过程时要检查 mysql.proc 表的 definer 字段的创建者是否为 root@localhost

select db, created, definer from mysql.proc;

如果 definer 字段有非 root@localhost 值,一定要改正!

update mysql.proc set definer = "root@localhost";

 

本文来源:http://www.gdgbn.com/shujuku/27643/