SQL Server stored procedures Return, output parameters and tips

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
 
Attached: SQL Server system global variables
@@CONNECTIONS 
Returns the number of connections since the last start or trying to connect.
@@CURSOR_ROWS 
Returns the number of qualifying rows connected to the open end of the cursor currently exist (number of rows of data valid line is opened to return the cursor has not been read)
@@DATEFIRST 
Returns the first day of the week numbers
@@ERROR 
The last execution of SQL statements that return an error code.
@@FETCH_STATUS 
Returns the state last cursor FETCH statement executed, rather than the status of any currently open connections cursor.
@@IDENTITY 
Returns the last inserted identity value
@@ langId 
Returns the current language native language identifier (ID).
@@LANGUAGE 
Returns the name of the language currently in use.
@@LOCK_TIMEOUT 
Returns the current session of the current lock timeout setting in milliseconds.
@@PROCID 
Stored procedure to return the current process identifier (ID).
@@ROWCOUNT 
Returns the number of rows affected by the last statement.
@@SERVERNAME 
Returns the local name server is running.
@@SPID 
Returns the current server process identifier (ID) of the user process.
@@ TRANCOUNT 
Returns the number of currently active transactions connected.
@@VERSION 
Return date, version and type of processor currently installed.
@@CPU_BUSY
SQL Server returns since the last start of their working time in milliseconds CPU
@@DATEFIRST
Returns SET DATEFIRST command DATAFIRST parameter is assigned the value of SET DATEFIRST, command to specify the first day of the week is the week
@@DBTS
Returns the current timestamp value database must ensure the database timestamp value is only
@@ERROR
Returns an error code is executed Transact-SQL statement
@@FETCH_STATUS
FETCH statement returns the last state value
@@IDLE
SQL Server returns since launch last the length of time the CPU is empty closed state unit of milliseconds
@@IO_BUSY
SQL Server returns since the last start time of the CPU to perform input and output operations it takes in milliseconds
@@ langId
Returns the language ID value currently being used
@@LANGUAGE
Returns the name of the language currently in use
@@LOCK_TIMEOUT
Returns the current session length of time waiting for a lock in milliseconds,
@@MAX_CONNECTIONS
Returns the maximum number of connections allowed to connect to SQL Server
@@MAX_PRECISION
Back accuracy and decimal numeric data type
@@NESTLEVEL
Returns the current execution of the stored series of nested initial value of 0
@@OPTIONS
Return information about the current SET options
@@PACK_RECEIVED
SQL Server returns the input packet by reading the number of network
@@PACK_SENT
SQL Server returns the number of output packets addressed to the network
@@PACKET_ERRORS
Returns the number of errors of network packets
@@PROCID
Returns the ID value stored procedure
@@ REMSERVER
Returns the name of a remote SQL Server database server
@@SERVICENAME
SQL Server is running to return to the state under which services such as MSSQLServer MSDTC SQLServerAgent
@@SPID
The server returns the current user ID value processing process
@@TEXTSIZE
Statement of the SET SET TEXTSIZE option value defines the maximum length statement SELECT statement type of text or image data substantially bytes
@@ TIMETICKS
Returns the number of microseconds each clock
@@TOTAL_ERRORS
The number of disk read and write error is returned
@@TOTAL_READ
Returns the number of disk read operations
@@TOTAL_WRITE
Returns the number of disk write operations
@@ TRANCOUNT
Returns the number of transactions in the current connection in the active state

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2013/04/07/3006433.html

Guess you like

Origin blog.csdn.net/weixin_33806509/article/details/93495365