mysql存储过程和存储函数初步
2014-09-02 00:00
393 查看
摘要: mysql存储过程和存储函数初步学习
1.存储过程 delimiter $ //首先声明自定义结束符 create procedure show_times() //存储过程名 begin //需要执行的语句 select 'local time is',current_timestamp; select 'UTC time is:',UTC_TIMESTAMP; end $ //结束 调用存储过程: call show_times()$ 2.带参数的存储过程 create procedure test_count(t int) begin select count(*) from t2 where t1=t; end $ 3.存储函数 delimiter $ ; create function test_count2(t int) returns int //返回一个int类型 reads sql data begin return (select count(*)from t2 where t1=t); end $ 调用存储函数:select test_count2(1)$ 4.---存储过程out参数 create procedure test_sum(out t2_sum int,out t1_sum int) begin select sum(t2) from t2 into t2_sum; select sum(t1) from t2 into t1_sum; end $ --调用 call test_sum(@sumT2,@sumT1)$ --取值 select @sumT2,@sumT1$ 5.--触发器 create trigger bt1 before insert on t2 for each row begin set new.dt=current_timestamp; if new.t1<0 then set new.t1=0; elseif new.t1>100 then set new.t1=100; end if; end $ 附表结构: CREATE TABLE `t2` ( `t2` int(11) NOT NULL AUTO_INCREMENT, `t1` int(11) DEFAULT NULL, `dt` datetime DEFAULT NULL, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`t2`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 6.存储函数必须遵守一条规则:不允许对调用本函数的表进行读和写的数据进行修改。 存储过程没有这个限制,但是如果存储过程在存储函数中调用就需要遵守这条规则。
相关文章推荐
- mysql存储过程基本函数
- (zz)mysql存储过程基本函数
- mysql5.0.22-log的内核数据库mysql误用mysql5.1.26-rc,引起存储过程与函数无法导入
- PHP调用mysql存储过程和函数的方法
- php调用mysql存储过程和函数的方法
- MySQL中建立操作函数和存储过程的命令
- mysql存储过程基本函数
- mysql 导入导出数据库以及函数、存储过程
- php调用mysql存储过程和函数的方法
- mysql存储过程基本语法和函数
- mysql存储过程基本函数
- MSSQL 和 Mysql 切割字符串 自定义函数与存储过程
- mysql存储过程基本函数
- mysql存储过程基本函数
- mysql存储过程基本函数
- php调用mysql存储过程和函数的方法
- mysql存储过程基本函数
- mysql存储过程学习笔记--常用函数
- mysql导出数据库及存储过程及函数
- MySQL存储过程常用的函数(字符串类型操作,数学类,日期时间类)