|
阅读:5960回复:0
sql server 存储过程的使用
存储过程:预先存储的SQL程序
存储过程分类: 系统存储过程(sp_) 扩展存储过程(xp_) 用户自定义存储过程(usp_) 定义: create proc 存储过程名称 [参数名 类型, 参数名2 类型] as SQL 语句块 go 执行: exec 存储名称 [参数] 执行时两种参数传值:1. 按照顺序 2. 指定参数名赋值(存储过程里的参数名相同)存储过程的参数分为两种:输入参数和输出参数 输入参数:用于向存储过程传入值,类似java语言或则c中的值传递。 输出参数:用于调用存储过程后,参会结果,类似java语言的按引用传递。 值传递和引用传递区别:
output:不能传入--Out是参数,传进、传出,缺一不可,在存储过程定义的时候一定要有out标识, --在调用该存储过程的时候也要有out标识 --OutPut则是相当于存储过程的返回值--不用传进,也不能传进 --它是在存储过程中定义,并且输出的 在程序中调用。out,output 是不一样 return 关键字的使用 if exists(select name from sysobjects where name='up_user' and type='p') drop proc up_user go create proc up_user @id int, @name varchar(20) output as declare @age int begin select @age=stuage,@name=stuname from stuinfo where uid=@id return @age end --执行该存储过程 declare @age int declare @name varchar(20) exec @age=up_user 2,@name output -- 存储过程的返回值将赋值给变量@age select @age,@name 注:SQL语句块里不要写执行存储过程语句 存储嵌套最多32层 |
|
|