10存儲過程觸發(fā)器_第1頁
10存儲過程觸發(fā)器_第2頁
10存儲過程觸發(fā)器_第3頁
10存儲過程觸發(fā)器_第4頁
10存儲過程觸發(fā)器_第5頁
已閱讀5頁,還剩93頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、存儲過程與函數(shù)的區(qū)別:存儲過程與函數(shù)的區(qū)別: 1. 1. 存儲過程可以返回存儲過程可以返回0 0個或多個參數(shù)值個或多個參數(shù)值( ( 也可也可執(zhí)行多個操作而無返回執(zhí)行多個操作而無返回) ),而函數(shù)只能返回單個值或,而函數(shù)只能返回單個值或表對象,表對象,函數(shù)中無法使用函數(shù)中無法使用selectselect只查詢而不處理其只查詢而不處理其值或是返回值值或是返回值; 2. 2. 函數(shù)可以嵌入到函數(shù)可以嵌入到sqlsql語句中執(zhí)行,如在語句中執(zhí)行,如在fromfrom子句中,而存儲過程只能使用子句中,而存儲過程只能使用execexec單獨調用。單獨調用。 3. 3.函數(shù)實現(xiàn)一些簡單功能,存儲過程相對功能

2、函數(shù)實現(xiàn)一些簡單功能,存儲過程相對功能強大些。強大些。 根據(jù)輸入的學號,計算學生的平均成績,并根據(jù)輸入的學號,計算學生的平均成績,并顯示績點(平均成績在顯示績點(平均成績在90-10090-100為為4 4個績點;平均成個績點;平均成績在績在80-9080-90為為3 3個績點;平均成績在個績點;平均成績在70-8070-80為為2 2個績個績點;平均成績在點;平均成績在60-7060-70為為1 1個績點;平均成績在個績點;平均成績在6060以下績點為以下績點為0 0),用函數(shù)、游標和存儲過程分別實),用函數(shù)、游標和存儲過程分別實現(xiàn)!現(xiàn)!顯示如下:顯示如下: 學號學號 平均成績平均成績 績點

3、績點create function f1(snotemp smallint) -用函數(shù)實現(xiàn)用函數(shù)實現(xiàn)returns newtab table (sno1 smallint,pjcj numeric(5,2),jd int)asbegin declare tempcj numeric(5,2),tempjd char(2) select tempcj=avg(grade) from sc where sno=snotemp set tempjd=case when tempcj=90 then 4 when tempcj=80 then 3 when tempcj=70 then 2 when

4、tempcj=60 then 1 else 0 end insert into newtab values(snotemp,tempcj,cast(tempjd as int) returnendselect * from dbo.f1(2)create proc p1 -用存儲過程實現(xiàn)用存儲過程實現(xiàn)1snotemp smallintasbegin declare tempcj numeric(5,2),tempjd char(2) select tempcj=avg(grade) from sc where sno=snotemp set tempjd=case when tempcj=90

5、 then 4 when tempcj=80 then 3 when tempcj=70 then 2 when tempcj=60 then 1 else 0 end print 課程號課程號+cast(snotemp as varchar(20)+ +平均成績平均成績+cast(tempcj as varchar(20)+ +績點績點+tempjdendexec p1 1create proc p2 -用存儲過程實現(xiàn)用存儲過程實現(xiàn)2,帶有輸出參數(shù),帶有輸出參數(shù)snotemp smallint,shuchu char(100) outputasbegin declare tempcj num

6、eric(5,2),tempjd char(2) select tempcj=avg(grade) from sc where sno=snotemp set tempjd=case when tempcj=90 then 4 when tempcj=80 then 3 when tempcj=70 then 2 when tempcj=60 then 1 else 0 end set shuchu= 課程號課程號+cast(cnotemp as varchar(20)+ +平均成平均成績績+cast(tempcj as varchar(20)+ +績點績點+tempjdenddeclare

7、tm char(100)exec p2 1,shuchu=tm outputprint tm-用用case語句實現(xiàn)每個學生的平均成績、績點語句實現(xiàn)每個學生的平均成績、績點select sno,avg(grade),case when avg(grade)=90 then 4 when avg(grade)=80 then 3 when avg(grade)=70 then 2 when avg(grade)=60 then 1 else 0 end from sc group by sno-游標實現(xiàn)游標實現(xiàn)declare a1 char(20),a2 floatdeclare c1 cursor for select sno,avg(grade) from sc group by snoopen c1fetch next from c1 into a1,a2while fetch_status =0beginselect a1, case when a2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論