How to Use Output-type Variable

--Create Procedure-- 

if object_id('outputtest') is not null 

drop proc outputtest 

Go 

 

create proc outputtest 

--This is proc declare, NOT variable declare-- 

@Output_student_lastfirst nvarchar(500) output, 

@Output_Student_number int output 

As 

--This is variable declare-- 

declare @FunctionVariable varchar(50) 

set @FunctionVariable='9' 

select Top 1 @Output_student_number=student_number,@Output_Student_LastFirst=lastfirst  

from students  

where grade_level=@FunctionVariable 

order by newid()  

--Select a student on random in grade 9-- 

select @Output_student_number as IN_PROC_STDNUM,@Output_Student_LastFirst AS IN_PROC_LASTFIRST 

--Create Procedure-- 

 
 

 
 

--Output Variables--  

declare @output_new_student_lastfirst nvarchar(500)  

declare @output_new_Student_number int  

exec outputtest   

--This is how to contact Procedure only when you have more than 2 variable to output--  

@Output_student_lastfirst=@output_new_student_lastfirst output 

@Output_Student_number=@output_new_Student_number output  

select   

@output_new_Student_number as OUTPUT_STUDNUM,  

@output_new_student_lastfirst as OUTPUT_LASTFIRST  

--Use these 2 variables as criteria--  

--Get full info about this student--  

select * from students s  

where s.lastfirst=@output_new_student_lastfirst and s.student_number=@output_new_Student_number 

 
----------------------------------------------------------------------------------------------------------------------------------------- 

----------------------------------------------------------------------------------------------------------------------------------------- 

 

--When you have only 1 Output variable in Procedure-- 

--Create Procedure--  

if object_id('outputtest') is not null  

drop proc outputtest  

go  

create proc outputtest  

--this is proc declare, NOT variable declare--  

@Output_Student_number int output  

As  

--this is variable declare--  

declare @FunctionVariable varchar(50)  

set @FunctionVariable='9'  

select Top 1 @Output_student_number=student_number 

from students   

where grade_level=@FunctionVariable  

order by newid()   

--select a student on random in grade 9--  

select @Output_student_number as IN_PROC_STDNUM 

--Create Procedure--  

 

 

 

 

 

--Output Variables--   

declare @output_new_Student_number int   

exec outputtest    

--This is how to contact Procedure only when you have only 1 variable to output--   

@output_new_Student_number output   

select    

@output_new_Student_number as OUTPUT_STUDNUM  

--Use these 2 variables as criteria--   

--Get full info about this student--   

select * from students s  

where s.student_number=@output_new_Student_number 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值