存储过程的简介以及简单实例
来源:互联网 发布:加入淘宝客的要求 编辑:程序博客网 时间:2024/06/05 20:21
存储过程简介:
我们把若干条sql语句封装起来,起个名字,----叫过程
我们把此过程存储在数据库中,-----存储过程
存储过程的优势:
1、存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL 语句每执行一次就编译一次,
所以使用存储过程可提高数据库执行速度。
2.当对数据库进行复杂操作时(如对多个表进行Update,Insert,Query,Delete 时),可将此复杂操作用存储过程封装起来
与数据库提供的事务处理结合一起使用。这些操作,如果用程序来完成,就变成了一条条的SQL语句,可能要多次连接数据
库。而换成存储,只需要连接一次数据库就可以了。
3.存储过程可以重复使用,可减少数据库开发人员的工作量。
4.安全性高,可设定只有某此用户才具有对指定存储过程的使用权。
创建语句:
DELIMITER $
create procedure procedureName()
begin
---sql语句
end$
查看已有的存储过程:
show PROCEDURE STATUS$
调用存储过程:
call p1()$
删除存储过程
drop PROCEDURE p2;
存储过程中使用条件语句:
DELIMITER $;
CREATE PROCEDURE P2(width int,height int)
BEGIN
SELECT CONCAT("面积是", width * height) as area;
IF width > height then
SELECT "fat";
ELSEIF height > width then
SELECT "thin";
ELSE
SELECT "fang";
end if;
END$
#调用存储过程,需要传递两个参数
call p2(2,3)$
利用whlie循环求1-100的和:
DELIMITER $;
CREATE PROCEDURE p3()
begin
DECLARE total int DEFAULT 0; #定义变量
DECLARE num int DEFAULT 0;
while num<100 do
set num := num +1;
set total := total + num;
end while;
SELECT CONCAT("1-100的和是",total);
end$
#调用
call p3()$
利用repeat循环来求1-100的和:
delimiter $;
create procedure p4()
begin
declare total int default 0;
declare i int default 0;
repeat
set i := i+1;
set total := total + i;
until i>=100 end repeat;
select total;
end$
case条件语句的用法:
delimiter $
create procedure p5()
begin
declare pos int default 0;
set pos := floor(5*rand());
case pos
when 1 then select "still flying";
when 2 then select "fall in sea";
when 3 then select "in the island";
else select "i don't know";
end case;
end$
用游标设置计数器来取出所有的数据:
#游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。
delimiter $;
create procedure p6()
begin
declare row_gid int default 0;
declare row_num int default 0;
declare row_name varchar(20);
declare cnt int default 0;
declare getgoods cursor for select gid,num,name from goods; #声明游标
select count(*) into cnt from goods;
open getgoods; #打开游标
repeat
set cnt :=cnt-1;
fetch getgoods into row_gid,row_num,row_name;
select row_gid,row_name;
until cnt<=0 end repeat;
close getgoods; #关闭游标
end$
我们把若干条sql语句封装起来,起个名字,----叫过程
我们把此过程存储在数据库中,-----存储过程
存储过程的优势:
1、存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL 语句每执行一次就编译一次,
所以使用存储过程可提高数据库执行速度。
2.当对数据库进行复杂操作时(如对多个表进行Update,Insert,Query,Delete 时),可将此复杂操作用存储过程封装起来
与数据库提供的事务处理结合一起使用。这些操作,如果用程序来完成,就变成了一条条的SQL语句,可能要多次连接数据
库。而换成存储,只需要连接一次数据库就可以了。
3.存储过程可以重复使用,可减少数据库开发人员的工作量。
4.安全性高,可设定只有某此用户才具有对指定存储过程的使用权。
创建语句:
DELIMITER $
create procedure procedureName()
begin
---sql语句
end$
查看已有的存储过程:
show PROCEDURE STATUS$
调用存储过程:
call p1()$
删除存储过程
drop PROCEDURE p2;
存储过程中使用条件语句:
DELIMITER $;
CREATE PROCEDURE P2(width int,height int)
BEGIN
SELECT CONCAT("面积是", width * height) as area;
IF width > height then
SELECT "fat";
ELSEIF height > width then
SELECT "thin";
ELSE
SELECT "fang";
end if;
END$
#调用存储过程,需要传递两个参数
call p2(2,3)$
利用whlie循环求1-100的和:
DELIMITER $;
CREATE PROCEDURE p3()
begin
DECLARE total int DEFAULT 0; #定义变量
DECLARE num int DEFAULT 0;
while num<100 do
set num := num +1;
set total := total + num;
end while;
SELECT CONCAT("1-100的和是",total);
end$
#调用
call p3()$
利用repeat循环来求1-100的和:
delimiter $;
create procedure p4()
begin
declare total int default 0;
declare i int default 0;
repeat
set i := i+1;
set total := total + i;
until i>=100 end repeat;
select total;
end$
case条件语句的用法:
delimiter $
create procedure p5()
begin
declare pos int default 0;
set pos := floor(5*rand());
case pos
when 1 then select "still flying";
when 2 then select "fall in sea";
when 3 then select "in the island";
else select "i don't know";
end case;
end$
用游标设置计数器来取出所有的数据:
#游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。
delimiter $;
create procedure p6()
begin
declare row_gid int default 0;
declare row_num int default 0;
declare row_name varchar(20);
declare cnt int default 0;
declare getgoods cursor for select gid,num,name from goods; #声明游标
select count(*) into cnt from goods;
open getgoods; #打开游标
repeat
set cnt :=cnt-1;
fetch getgoods into row_gid,row_num,row_name;
select row_gid,row_name;
until cnt<=0 end repeat;
close getgoods; #关闭游标
end$
0 0
- 存储过程的简介以及简单实例
- 存储过程简单实例
- 存储过程简单实例
- Sql存储过程的简单实例
- 存储过程的一个简单实例
- Mysql创建存储过程的简单实例
- 分页存储过程简单实例
- Oracle存储过程简单实例
- Oracle存储过程简单实例
- 简单SQL存储过程实例
- Oracle存储过程简单实例
- Oracle存储过程简单实例:
- Mybatis简单存储过程实例
- ibatis的简介以及简单的应用实例
- Servlet的学习(零)-简介以及简单实例
- pl/sql 有返回值的存储过程 以及代码中调用存储过程 实例
- 数据库存储过程简介与实例
- C#调用oracle存储过程 最简单的实例
- 贪心 - poj3122 Pie
- listfragment 中的 listview 不能运行
- 0630.md
- Android Studio系列教程一--下载和安装
- C#程序设计实验考试
- 存储过程的简介以及简单实例
- 深入Javascript函数与闭包(执行环境、变量对象与作用域链)详解
- IOC--UI--LessonView
- 文章标题
- TexturePacker使用
- git查看历史与乱码解决
- 写一个通用的事件侦听器函数(兼容多浏览器)
- No sources that matches the given name (at 'layout_toLeftOf' with value '@id/
- iOS7新特性 ViewController转场切换(三) 自定义视图控制器容器的切换---非交互式