1. 程式人生 > >SQL第六章(儲存過程)

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

[email protected] end

go