1. 程式人生 > >T-SQL 帶引數儲存過程

T-SQL 帶引數儲存過程

建立帶引數的儲存過程

 1 use StudentManager
 2 go
 3 if exists(select * from sysobjects where name='usp_ScoreQuery4')
 4 drop procedure usp_ScoreQuery4
 5 go
 6 create procedure usp_ScoreQuery4 --建立帶引數的儲存過程
 7 @AbsentCount int output,--缺考總人數
 8 @FailedCount int output,--不及格總人數
 9 @CSharp int=60,
10 @DB int=60
11 as 12 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 13 from Students 14 inner join ScoreList on Students.StudentId=ScoreList.StudentId 15 where CSharp<@CSharp or SQLServerDB<@DB --顯示結果列表 16 select @AbsentCount
=count(*) from Students 17 where StudentId not in(select StudentId from ScoreList) --查詢缺考總人數 18 select @FailedCount=count(*) from ScoreList 19 where CSharp<@CSharp or SQLServerDB<@DB --查詢不及格總人數 20 go

呼叫

1 use StudentManager
2 go
3 --呼叫帶引數的儲存過程
4
declare @AbsentCount int,@FailedCount int --首先定義輸出引數 5 exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output 6 --使用反饋的結果 7 select 缺考總數=@AbsentCount,不及格總數=@FailedCount

呼叫時傳入輸入引數

1 use StudentManager
2 go
3 --呼叫帶引數的儲存過程
4 declare @AbsentCount int,@FailedCount int --首先定義輸出引數
5 exec usp_ScoreQuery4 @AbsentCount output,@FailedCount output ,65,70
6 --使用反饋的結果
7 select 缺考總數=@AbsentCount,不及格總數=@FailedCount