T-SQL 有参数存储过程的创建与执行

 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   --两个参数都是用默认参数

猜你喜欢

转载自www.cnblogs.com/Spinoza/p/10051258.html