温故而知新--sql存储过程复习

存储过程是已编译好的T-SQL语句的集合,可以随时调用,速度快,不易出错。但是写长了 确实是很难维护,在项目中我也不怎么喜欢使用。

实例1   可以传递参数,普通参数和输出参数(output)

create proc Newpro

@testVarA int,

@testVatB int,

@testSum int Output

as

begin

set @testSum=@testVarA+@testVarB

end

调用存储过程Newpro

declare @testA int

execute Newpro 100,200,@testA output

print @testA

实例2

create proc testUser

@testUserName varchar(30),

@testPassWord varchar(30)

as

begin

declare @testMsg varchar(100)

if @testUserName='user1'

     begin

     if @testPassWord='123'

     set @testMsg='欢迎进入'

     else

     set @testMsg='对不起,密码错误'

     end

else if @testUserName='user2'

     begin

      if @testPassWord='abc'

      set @testMsg='欢迎进入'

      else

      set @testMsg='对不起,密码错误'

     end

else

      set @testMag='请输入正确的用户名'

print @testMsg

end

调用存储过程testUser

exec testUser 'user1','123'

原文地址:https://www.cnblogs.com/tdws/p/4063006.html