SQL第六章(存储过程)

1、存储过程的优点

①允许模块化程序设计

②执行速度更快

③减少网络流通量

④提高系统安全性

2、存储过程的分类

①系统存储过程

②用户自定义存储过程

3、常用的系统存储过程

SQL第六章(存储过程)

SQL第六章(存储过程)

 

SQL第六章(存储过程)

4、使用存储过程

①定义存储过程分语法

        created proc  存储过程名

         as

                 T- SQL语句

         go

 

 

②调用语法

      exec  过程名   【参数】

5、创建不带参数的存储过程

if exists(select*from sys.procedures where name='pr_stu_mark')
drop proc pr_stu_mark
go

create proc pr_stu_mark
as
    select StuInfo.stuid,stuname,stusex,subject,score
    from StuInfo,StuMarks
    where StuInfo.stuid=StuMarks.stuid

go

 

--查询存储过程
--exec dbo.存储过程名字
exec dbo.pr_stu_mark '李四'

 

6、创建带参数的存储过程

存储过程的参数分两种

    ①输入参数   用于想存储过程传入值,

    ②输出参数   用于在调用存储过程后,返回结果

if exists(select*from sys.procedures where name='pr_stu_mark')
drop proc pr_stu_mark
go

create proc pr_stu_mark(@name varchar(10)=null)
as
if @name is null
begin
    select StuInfo.stuid,stuname,stusex,subject,score
    from StuInfo,StuMarks
    where StuInfo.stuid=StuMarks.stuid
end
else
begin
    select StuInfo.stuid,stuname,stusex,subject,score
    from StuInfo,StuMarks
    where StuInfo.stuid=StuMarks.stuid and [email protected]
end

go