1.创建函数语法 mysql>delimiter
// mysql>create function 函数名(参数1
参数1类型,...) returns 返回类型 >begin >return 返回值; >end >// mysql>select 函数名(参数1,
参数2, 。。。); >// |
下面是创建存储过程的一个例子(都是在navicat的命令行模式F6下执行)1. 先建表,先F6进入到命令行模式,然后进入到你要进行操作的数据库,然后输入下面的语句,创建一个表user
(1) 先执行:show databases;查看所以数据库
(2) 再执行:use 数据库名;进入到你要操作的数据库中
(3) 然后输入建表的语句如下
create table new_user(id
mediumint(8) unsigned not null auto_increment,name char(15)not null default "",passchar(32)not null default "",note
text not null,primary key (id))engine=Innodb
charset=utf8;
2、在新建的表中插入一条数据
insert into new_user(nsme,
pass, note) values('hmj','123','ok');3、新建存储过程proc_name,按照下列输入命令即可mysql>delimiter
//mysql>create procedure proc_name
(in parameterinteger)mysql>beginmysql>if
parameter=0 thenmysql>select *from new_user order by idasc;mysql>elsemysql>select *from new_user order by iddesc;mysql>end if;mysql>end;mysql>//
4、执行存储过程
执行:mysql>call
proc_name(0);mysql>//
下面就是命令执行的一些截图:
|
在上面创建好了存储过程之后,我们就可以随时拿来使用了,可以查看一下新建的存储过程
执行存储过程有两种方式:
1、想要执行哪个存储过程就直接在命令行模式下输入:call 存储过程名(参数),如上例的:
call proc_name(0);
2、或者右击存储过程,选择“运行函数”,然后再输入参数,就可以运行这个存储过程了。
结果和在命令行下执行的一致。
本文介绍如何在MySQL中创建存储过程和函数,并通过具体实例演示其使用方法。

4286

被折叠的 条评论
为什么被折叠?



