create proc StuProc
as //此處 as 不能夠省略不寫
begin //begin 和 end 是一對,不能夠只寫其中一個,但能夠都不寫
select S#,Sname,Sage,Ssex from student
end
go
as //此處 as 不能夠省略不寫
begin //begin 和 end 是一對,不能夠只寫其中一個,但能夠都不寫
select S#,Sname,Sage,Ssex from student
end
go
create proc StuProc
@sname varchar(100)
as
begin
select S#,Sname,Sage,Ssex from student where sname=@sname
end
go
exec StuProc '趙雷' //執行語句
@sname varchar(100)
as
begin
select S#,Sname,Sage,Ssex from student where sname=@sname
end
go
exec StuProc '趙雷' //執行語句
上面是在外部給變量賦值,也能夠在內部直接給變量設置默認值post
create proc StuProc
@sname varchar(100)='趙雷'
as
begin
select S#,Sname,Sage,Ssex from student where sname=@sname
end
go
exec StuProc
@sname varchar(100)='趙雷'
as
begin
select S#,Sname,Sage,Ssex from student where sname=@sname
end
go
exec StuProc
也能夠把變量的內容輸出,使用outputurl
create proc StuProc
@sname varchar(100),
@IsRight int output //傳出參數
as
if exists (select S#,Sname,Sage,Ssex from student where sname=@sname)
set @IsRight =1
else
set @IsRight=0
go
declare @IsRight int
exec StuProc '趙雷' , @IsRight output
select @IsRight
@sname varchar(100),
@IsRight int output //傳出參數
as
if exists (select S#,Sname,Sage,Ssex from student where sname=@sname)
set @IsRight =1
else
set @IsRight=0
go
declare @IsRight int
exec StuProc '趙雷' , @IsRight output
select @IsRight
以上是全局變量,下面來了解局部變量
局部變量也稱爲內部變量。局部變量是在函數內做定義說明的。其做用域僅限於函數內部,離開該函數後再使用這種變量是非法的。
局部變量的定義:必須先用Declare命令定之後纔可使用,declare{@變量名 數據類型}
局部變量的賦值方法:set{@變量名=表達式}或者select{@變量名=表達式}
局部變量的顯示:select @變量名spa
create proc StuProc
as
declare @sname varchar(100)
set @sname='趙雷'
select S#,Sname,Sage,Ssex from student where sname=@sname
go
exec StuProc
as
declare @sname varchar(100)
set @sname='趙雷'
select S#,Sname,Sage,Ssex from student where sname=@sname
go
exec StuProc
那若是是要把局部變量的數據顯示出來怎麼辦呢?code
create proc StuProc
as
declare @sname varchar(100)
set @sname=(select Sname from student where S#=01)
select @sname
go
exec StuProc
as
declare @sname varchar(100)
set @sname=(select Sname from student where S#=01)
select @sname
go
exec StuProc
帶輸出參數存儲過程htm
if (object_id('proc_getStudentRecord', 'P') is not null) drop proc proc_getStudentRecordgocreate proc proc_getStudentRecord( @id int, --默認輸入參數 @name varchar(20) out, --輸出參數 @age varchar(20) output--輸入輸出參數)as select @name = name, @age = age from student where id = @id and sex = @age;go