1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery2') 4 drop procedure usp_ScoreQuery2 5 go 6 --創建帶參數的存儲過程 7 create procedure usp_ScoreQuery2 8 @CSharp int, 9 @DB int 10 as 11 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 12 from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 where CSharp<@CSharp or SQLServerDB<@DB 15 go 16 --調用帶參數的存儲過程 17 exec usp_ScoreQuery2 60,65 --按照參數順序賦值 18 exec usp_ScoreQuery2 @DB=65,@CSharp=60 --參數順序可以調換
為參數賦默認值
1 use StudentManager 2 go 3 if exists(select * from sysobjects where name='usp_ScoreQuery3') 4 drop procedure usp_ScoreQuery3 5 go 6 --創建帶參數的存儲過程 7 create procedure usp_ScoreQuery3 8 @CSharp int=60, 9 @DB int=60 10 as 11 select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB 12 from Students 13 inner join ScoreList on Students.StudentId=ScoreList.StudentId 14 where CSharp<@CSharp or SQLServerDB<@DB 15 go 16 --調用帶參數的存儲過程 17 exec usp_ScoreQuery3 65 --第二個參數沒有賦值,則默認 18 exec usp_ScoreQuery3 @DB=65 19 exec usp_ScoreQuery3 default,65 --不使用顯示方式賦值 20 exec usp_ScoreQuery3 --兩個參數都是用默認參數
