sql server stored procedure and return to use output, Detailed

SQL Server is now becoming one of the most important operating system, database management system WindowNT above, with the introduction of SQL Server2000, Microsoft's such a database service system truly realized in WindowsNT / 2000 family of operating systems dominance in Microsoft's operating system, without any kind of database system can compete with them, including the database world leader in Oracle's Oracle database, including housekeeping. Admittedly, the biggest drawback is that SQL Server only runs on Microsoft's own operating system, and this is a fatal point of SQL Server. On the other hand it has become the best promoters to promote their own on top of SQL Server only "land" will play its functions to the extreme maximize the use of a variety of potential NT family of operating systems! As important SQL Server database system, a concept that is stored procedure, rational use of stored procedures, can effectively improve the performance of the program; and the business logic is encapsulated stored procedures in the database system can greatly improve the entire software system maintainability, when your business logic changes, eliminating the need to modify and compile the client application and redistribute them to the hands of a large number of users, you only need to modify the corresponding business logic on the server side implementation of a stored procedure that is can. Write your own reasonable needs of stored procedures, you can maximize the use of resources of SQL Server. Let us look at a variety of skill experience writing SQL Server stored procedures and use of stored procedures.
 
Input This parameter is only used to transfer information from the application to the stored procedure. 
InputOutput This parameter can transfer information from the application to the stored procedure and the process of transmitting information from the storage back to the application. 
Output This parameter is only used for the application of information transmitted back from a stored procedure. 
This parameter represents the return value ReturnValue stored procedure. SQL Server stored procedure parameter list is not displayed in this parameter. RETURN statement with a stored procedure only in the values ​​associated.
  After the primary key stored procedure to generate a new value, typically using the RETURN statement in the stored procedure returns the value, used to access the parameter value type is ReturnValue parameters. 
 
1, with no simple procedure stored input parameters
if object_id('up_user') is not null
drop proc up_user
go
create proc up_user
as
set nocount on
delcare @name varchar(10)
begin
select @name=uname from user
end
set nocount off
go
esec up_user
 
2, the stored procedure with a simple input parameters
 if object_id('up_user') is not null
drop proc up_user
go
create proc up_user
@id int
as
set nocount on
delcare @name varchar(10)
begin
select @name=uname from user where  uid=@id
end
set nocount off
go
- execute the stored procedure
esec up_user 1
 
3, the stored procedure with parameters Return
 if object_id('up_user') is not null
drop proc up_user
go
create proc up_user
as
set nocount on
delcare @age int
begin
select @age=uage from user
return @age
end
set nocount off
go
 
- execute the stored procedure
declare @age int
exec @age=up_user
select @age
 
4, the output parameter is a stored procedure with
 if object_id('up_user') is not null
drop proc up_user
go
create proc up_user
@id int,
@name varchar(10) ='' output
as
set nocount on
begin
select @name=uname from user where uid=@id
end
set nocount off
go
 
- execute the stored procedure
declare @name varchar(10)
exec up_user 2, @name output
select @name
 
5, while the stored procedure with the output parameter and the 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
 
- execute the stored procedure
declare @age int
declare @name varchar(20)
exec @age=up_user 2,@name output
select @age,@name
 
附:SQL Server 系统全局变量
@@CONNECTIONS 
返回自上次启动以来连接或试图连接的次数。
@@CURSOR_ROWS 
返回连接上最后打开的游标中当前存在的合格行的数量(返回被打开的游标中还未被读取的有效数据行的行数)
@@DATEFIRST 
返回每周第一天的数字
@@ERROR 
返回最后执行的SQL 语句的错误代码。
@@FETCH_STATUS 
返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。
@@IDENTITY 
返回最后插入的标识值
@@LANGID 
返回当前所使用语言的本地语言标识符(ID)。
@@LANGUAGE 
返回当前使用的语言名。
@@LOCK_TIMEOUT 
返回当前会话的当前锁超时设置,单位为毫秒。
@@PROCID 
返回当前过程的存储过程标识符 (ID) 。
@@ROWCOUNT 
返回受上一语句影响的行数。
@@SERVERNAME 
返回运行 的本地服务器名称。
@@SPID 
返回当前用户进程的服务器进程标识符 (ID)。
@@TRANCOUNT 
返回当前连接的活动事务数。
@@VERSION 
返回当前安装的日期、版本和处理器类型。
@@CPU_BUSY
返回自SQL Server 最近一次启动以来CPU 的工作时间其单位为毫秒
@@DATEFIRST
返回使用SET DATEFIRST 命令而被赋值的DATAFIRST 参数值SET DATEFIRST,命令用来指定每周的第一天是星期几
@@DBTS
返回当前数据库的时间戳值必须保证数据库中时间戳的值是惟一的
@@ERROR
返回执行Transact-SQL 语句的错误代码
@@FETCH_STATUS
返回上一次FETCH 语句的状态值
@@IDLE
返回自SQL Server 最近一次启动以来CPU 处于空闭状态的时间长短单位为毫秒
@@IO_BUSY
返回自SQL Server 最近一次启动以来CPU 执行输入输出操作所花费的时间其单位为毫秒
@@LANGID
返回当前所使用的语言ID 值
@@LANGUAGE
返回当前使用的语言名称
@@LOCK_TIMEOUT
返回当前会话等待锁的时间长短其单位为毫秒
@@MAX_CONNECTIONS
返回允许连接到SQL Server 的最大连接数目
@@MAX_PRECISION
返回decimal 和numeric 数据类型的精确度
@@NESTLEVEL
返回当前执行的存储过程的嵌套级数初始值为0
@@OPTIONS
返回当前SET 选项的信息
@@PACK_RECEIVED
返回SQL Server 通过网络读取的输入包的数目
@@PACK_SENT
返回SQL Server 写给网络的输出包的数目
@@PACKET_ERRORS
返回网络包的错误数目
@@PROCID
返回当前存储过程的ID 值
@@REMSERVER
返回远程SQL Server 数据库服务器的名称
@@SERVICENAME
返回SQL Server 正运行于哪种服务状态之下如MSSQLServer MSDTC SQLServerAgent
@@SPID
返回当前用户处理的服务器处理ID 值
@@TEXTSIZE
返回SET 语句的TEXTSIZE 选项值SET 语句定义了SELECT 语句中text 或image数据类型的最大长度基本单位为字节
@@TIMETICKS
返回每一时钟的微秒数
@@TOTAL_ERRORS
返回磁盘读写错误数目
@@TOTAL_READ
返回磁盘读操作的数目
@@TOTAL_WRITE
返回磁盘写操作的数目
@@TRANCOUNT
返回当前连接中处于激活状态的事务数目

Guess you like

Origin www.cnblogs.com/baili-luoyun/p/11113232.html