给tom用户赋予在任何表,任何数据库都有创建存储过程的权限
-- sql编程
declare 声明局部变量: 只能用 begin ....end
局部变量的定义:
全局变量的定义:
这个全局变量的结果会直接打印在屏幕上:
SQL编程
变量
局部变量 declare
全局变量 @a
set @a =10; 直接赋值
select @c :=‘abc‘; 打印并赋值 全局变量
select @@version; 查看数据库的版本
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
在declare 之前不能有非declare 语句
declare a int;
declare b,c,d float default 4.5;
set a=10;
set b=1.23,c=8.9;
select a,b,c,@b;
end//
delimiter ;
show procedure status\G;
drop procedure if exists sp1()
demiliter //
create procedure sp1()
begin
declare a int;
declare b dec(5,2) default 10;
declare c varchar(32) charset utf8;
set a=10;
set b=314.12;
set c =‘中国‘;
select a,b,c;
end //
declare ;
call sp1();调用存储程序里的数据
drop procedure if exists sp1;
delimiter //
create procedure sp1(name varchar(32),age int)
begin
create table if not exists stu6(
stuID int auto_increment,
stuName varchar(32),
stuAge int,
primary key(stuID))engine=innoDB charset = utf8;
insert into stu6(stuName,stuAge) values(name,age);
end //
delimiter ;
call sp1(‘baba‘,54);
-- 使用out传出值,,,,,inout传入传出
drop procedure if exists sp1;
delimiter //
create procedure sp1(out c int)
begin
select count(1) into c from stu;
end //
delimiter ;
call sp1(@a);
select @a;===========select count(*) from stu;
使用php连接数据库
create table dept2
(
deptID int,
deptName varchar(32)
)engine=innodb charset=utf8;
drop procedure if exists sp1;
delimiter //
create procedure sp1(a int, b varchar(32))
begin
insert into dept values(a, b);
end //
delimiter ;
drop procedure if exists sp3;
delimiter //
create procedure sp3(inout a int)
begin
select count(1) into a from stu where stuName = ‘张三‘;
end//
delimiter ;
set @x = 20;
call sp1(@x);
select @x;
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
create table if not exists emp
(
empID int,
empName varchar(32),
primary key(empID)
)engine=innodb charset=utf8;
insert into emp values(1, ‘张三‘), (2, ‘李四‘);
end//
delimiter ;
call sp1();
-- 给sp传值
drop procedure if exists sp1;
delimiter //
create procedure sp1(a int)
begin
declare x int;
declare y varchar(32);
select stuID, stuName into x, y from stu where age > a order by stuID limit 1;
select x, y;
end//
delimiter ;
call sp1(20);
select @x;
-- 如何存值
stu deptID = 1;
select stuID, stuName into @x, @y from stu where age > 20 order by stuID limit 1;
select @x := stuID, @y := stuName from stu where age > 20 order by stuID limit 1; -- 只能赋给全局变量
declare x int;
delcare y varchar(32);
select stuID, stuName into x, y from stu where age > 20 order by stuID limit 1;
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
declare a int;
declare b varchar(32);
select a := stuID, b := stuName from stu where age in(20, 30) order by stuID limit 1;
select a, b;
end//
delimiter ;
call sp1();
-- 把值存起来
sp在实际工作中的应用:
写数据
users(userID, userName, pwd);
-- 身份验证用sp来做
-- php, 写fun
-- sql编程
--if
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
select salary into @a from stu where stuID = 1;
case
when @a >= 5000 && @a <= 6000 then
select ‘不错‘;
when @a >= 4000 and @a < 5000 then
select ‘还可以‘;
else
select ‘还行‘;
end case;
end//
delimiter ;
call sp1();
-- case, 事件
-- 搜索格式 case
case when @a between 80 and 100 then
select ‘优秀‘;
select ‘加油‘;
when @a >= 60 && @a < 80 then
select ‘及格‘;
else
select ‘不及格‘;
end case;
-- 简单格式
help case
1. 操作符
2. 语句块
操作符:
set @a = 10;
select case @a when 10 then ‘返回1‘ when 20 then ‘返回2‘ else ‘返回3‘ end;
select case age when 40 then salary * 1.1 when 30 then salary * 1.2 else salary * 1.3 end from stu;
select case when @a >= 80 && @a <= 100 then ‘优秀‘ when @a >= 60 && @a < 80 then ‘及格‘ else ‘不及格‘;
语句块, 出现在sp中
简单格式:
case @a
when 6000 then
select ‘不错‘;
when 5000 then
select ‘还可以‘;
when 4000 then
select ‘过得去‘;
else
select ‘还行‘;
end case;
搜索格式:
select salary into @a from stu where stuID = 1;
case
when @a >= 5000 && @a <= 6000 then
select ‘不错‘;
when @a >= 4000 and @a < 5000 then
select ‘还可以‘;
else
select ‘还行‘;
end case;
-- 判断
-- 循环 if
-- 循环中的leave、iterate
-- while repeat...until loop
-- while
-- break, continue
while:
set @a = 1;
ds:while @a <= 10
do
if @a % 2 = 0 then
set @a = @a + 1;
iterate ds;
end if;
select @a;
set @a = @a + 1;
end while;
-- do while
-- repeat, 相当于do...while
set @a = 1;
repeat
select @a;
set @a = @a + 1;
until @a > 5 end repeat;
-- loop
drop procedure if exists sp1;
delimiter //
create procedure sp1()
begin
set @a = 1;
aix:loop
if @a > 10 then
leave aix;
elseif mod(@a, 2) = 0 then
set @a = @a + 1;
iterate aix;
end if;
select @a;
set @a = @a + 1;
end loop;
end//
delimiter ;
call sp1();
-- loop循环
if then
...
else
...
end if
case
when @a > 1 and @a < 10 then
...
else
...
end case;
-- sql编程中判断, 循环
if, case
while, rpeat...until, loop(if leave)
sp
function
-- mysql自定义fun
可以传参数, 可不传
不能传出参数
out a int, inout b int
fun, 一定有return值
与sp, 是db的一种object, select ds.fun1();
fun1(10, 20) -> 30
select fun1(10, 20); -> 30
select fun1(); -> hello world!
drop function if exists fun1;
delimiter //
create function fun1(a double, b double)
returns double
begin
return a + b;
end//
delimiter ;
select fun1();
------if
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a = 80.5;
case @a
when 80 then select ‘abc‘;
when 90 then select ‘def‘;
else select ‘xyz‘;
end case;
end //
delimiter ;
call sp();
mysql59>call sp();
+-----+
| xyz |
+-----+
| xyz |
+-----+
1 row in set (0.00 sec)
-------do ....while
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a = 1;
while @a <= 5
do
select @a;
set @a = @a + 1;
end while;
end //
delimiter ;
mysql59>call sp();
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.02 sec)
+------+
| @a |
+------+
| 2 |
+------+
1 row in set (0.02 sec)
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.02 sec)
+------+
| @a |
+------+
| 4 |
+------+
1 row in set (0.02 sec)
+------+
| @a |
+------+
| 5 |
+------+
1 row in set (0.02 sec)
-------do while
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a =3;
while @a<= 10
do
insert into stu(stuID,stuAddr) values(@a,‘zz‘);
set @a= @a + 1;
end while;
end //
delimiter ;
vim test.sh
#!/bin/bash
a=1
while((a<=5))
do
echo $a;
(( a++ ))
done
------leave
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a = 1;
sx:while @a <=10
do
if @a =3 then
leave sx;
end if;
select @a;
set @[email protected] + 1;
end while;
end //
delimiter ;
mysql59> call sp();
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
+------+
| @a |
+------+
| 2 |
+------+
-------- iterate
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a =1;
ds:while @a<=10
do
if @a =3 then
set @[email protected] +1;
iterate ds;
end if;
select @a;
set @a [email protected] +1;
end while;
end //
delimiter ;
--------------loop
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a = 1;
aix:loop
if @a > 6 then
leave aix;
end if;
select @a;
set @a = @a + 1;
end loop;
end //
delimiter ;
mysql59>call sp();
]+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.04 sec)
+------+
| @a |
+------+
| 2 |
+------+
1 row in set (0.04 sec)
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.04 sec)
+------+
| @a |
+------+
| 4 |
+------+
1 row in set (0.04 sec)
+------+
| @a |
+------+
| 5 |
+------+
1 row in set (0.04 sec)
+------+
| @a |
+------+
| 6 |
+------+
1 row in set (0.04 sec)
--------repeat
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a =1;
repeat
select @a;
set @a = @a +1;
until @a > 3 end repeat;
end //
delimiter ;
mysql59>call sp();
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
+------+
| @a |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
------loop leave iterate
drop procedure if exists sp;
delimiter //
create procedure sp()
begin
set @a =1;
aix:loop
if @a > 6 then
leave aix;
elseif @a % 2=0 then
set @a = @a + 1;
iterate aix;
end if;
select @a;
set @a [email protected] +1;
end loop;
end //
delimiter ;
------function
drop function if exists fun1;
delimiter //
create function fun1()
returns varchar(32)
begin
return ‘hello world!‘;
end //
delimiter ;
mysql59>select fun1();
+--------------+
| fun1() |
+--------------+
| hello world! |
+--------------+
1 row in set (0.05 sec)
drop function if exists fun1;
delimiter //
create function fun1(a double,b double)
returns double
begin
declare c double;
set c = a +b;
return c;
end //
delimiter ;
select fun1(2.3,5.1);
mysql59>select fun1(2.3,5.1);
+--------------------+
| fun1(2.3,5.1) |
+--------------------+
| 7.3999999999999995 |
+--------------------+
1 row in set (0.04 sec)
mysql59>show function status\G
*************************** 1. row ***************************
Db: haha
Name: fun1
Type: FUNCTION
Definer: [email protected]
Modified: 2013-07-22 21:06:28