将常用的或很复杂的工作,预先写好并用一个指定的名称存储起来,以后数据库要提供与已定义的功能相同的服务时,只需调用执行即可。存储过程就是这种预先编译好的程序块,通常由一系列SQL语句构成,但不局限于SQL语句。
与一般的SQL语句相比,存储过程具有这样一些优点:
1)便于模块化程序设计
2)提高执行速度
3)减少网络流量
4)可作为安全机制使用
1、创建存储过程
create procedure pro1 @name varchar(6) as begin select customers.cname,products.pname,oadd from customers,products,orders where customers.cno=orders.cno and products.pno=orders.pno and customers.cname=@name end
create procedure pro2 @name varchar(6), @sum tinyint output as begin select @sum=sum(oquant) from customers,products,orders where customers.cno=orders.cno and products.pno=orders.pno and customers.cname=@name end
2、执行存储过程
declare @sum tinyint execute pro2 '吴丹',@sum1 output print @sum1
3、存储过程的更改与删除
create procedure pro1 @name varchar(6), @sum1 tinyint output, @sum2 smallmoney output as begin select cname as '姓名',pname as '商品',otel as '送货电话',oadd as '送货地址',oquant as '数量',price as '单价'from customers,products,orders where customers.cno=orders.cno and products.pno=orders.pno and customers.cname=@name select @sum1=sum(oquant),@sum2=sum(price*oquant) from customers,products,orders where customers.cno=orders.cno and products.pno=orders.pno and customers.cname=@name
end
declare @sum1 tinyint declare @sum2 smallmoney execute pro1 '吴丹',@sum1 output,@sum2 output print @sum1+','+space(2)+str(@sum2,8,2)
drop procedure pro2