SQL第六章(存儲過程)

1、存儲過程的優點

①允許模塊化程序設計

②執行速度更快

③減少網絡流通量

④提高系統安全性

2、存儲過程的分類

①系統存儲過程

②用戶自定義存儲過程

3、常用的系統存儲過程

 

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 stuName=@name
end

go

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章