Detailed explanation of stored procedures


http://www.cnblogs.com/knowledgesea/archive/2013/01/02/2841588.html

Introduction to Stored Procedures


What is a stored procedure : A stored procedure can be said to be a recordset. It is a code block composed of some T-SQL statements. These T-SQL statement codes implement some functions like a method (addition to a single table or multiple tables). Delete, modify and check), and then give this code block a name, and call it when this function is used.

The benefits of the stored procedure :

1. Because the database executes the action, it is first compiled and then executed. However, the stored procedure is a compiled code block, so the execution efficiency is higher than the T-SQL statement.

2. A stored procedure can replace a large number of T-SQL statements when the program interacts in the network, so it can also reduce the network traffic and improve the communication rate.

3. Through the stored procedure, users without authority can access the database indirectly under control, thereby ensuring the security of the data.

Summary: In short, stored procedures are good things, and they are a must-have tool when doing projects. The basic syntax of stored procedures is described below.


Stored procedure syntax and parameter explanation


Some basic syntax of stored procedures:

copy code
copy code
-- ------------Create Stored Procedure-------

CREATE PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } [ VARYING ] [ = default ] [ OUTPUT ] ] [ ,...n ] [ WITH { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ] [ FOR REPLICATION ] AS sql_statement [ ...n ] -- ------------ call stored procedure ----------------- EXECUTE Procedure_name '' -- stored procedure If there is a parameter, the format of the parameter is: @parameter name=value, or it can be directly the parameter value value -------------- delete stored procedure ----------------- drop procedure procedure_name --can call another stored procedure in a stored procedure, but not delete another stored procedure
copy code
copy code

Parameters for creating a stored procedure:
1. procedure_name  : the name of the stored procedure, add # in front of it for a local temporary stored procedure, and add ## for a global temporary stored procedure.

2.; number : is an optional integer used to group procedures with the same name, so that procedures in the same group can be removed together with a single DROP PROCEDURE statement. For example, procedures used by an application named orders could be named orderproc;1, orderproc;2, and so on. The DROP PROCEDURE orderproc statement will drop the entire group. If the name contains a delimited identifier, numbers should not be included in the identifier, only appropriate delimiters should be used before and after procedure_name. 

3. @parameter : The parameter of the stored procedure. There can be one or more. The user must supply a value for each declared parameter when executing the procedure (unless a default value for that parameter is defined). Stored procedures can have up to 2.100 parameters.
Use the @ symbol as the first character to specify parameter names. Parameter names must conform to the rules for identifiers. Parameters for each procedure are used only for that procedure itself; the same parameter names can be used in other procedures. By default, parameters can only be used in place of constants, not in place of table, column, or other database object names. See EXECUTE for more information. 

4.data_type : The data type of the parameter. All data types, including text, ntext, and image, can be used as stored procedure parameters. However, the cursor data type can only be used for OUTPUT parameters. If the specified data type is cursor, the VARYING and OUTPUT keywords must also be specified. For more information about the data types provided by SQL Server and their syntax, see Data Types. 
说明 对于可以是 cursor 数据类型的输出参数,没有最大数目的限制。 

5.VARYING: 指定作为输出参数支持的结果集(由存储过程动态构造,内容可以变化)。仅适用于游标参数。 

6.default: 参数的默认值。如果定义了默认值,不必指定该参数的值即可执行过程。默认值必须是常量或 NULL。如果过程将对该参数使用 LIKE 关键字,那么默认值中可以包含通配符(%、_、[] 和 [^])。

7.OUTPUT :表明参数是返回参数。该选项的值可以返回给 EXEC[UTE]。使用 OUTPUT 参数可将信息返回给调用过程。Text、ntext 和 image 参数可用作 OUTPUT 参数。使用 OUTPUT 关键字的输出参数可以是游标占位符。 

8.RECOMPILE: 表明 SQL Server 不会缓存该过程的计划,该过程将在运行时重新编译。在使用非典型值或临时值而不希望覆盖缓存在内存中的执行计划时,请使用 RECOMPILE 选项。

9.ENCRYPTION: 表示 SQL Server 加密 syscomments 表中包含 CREATE PROCEDURE 语句文本的条目。使用 ENCRYPTION 可防止将过程作为 SQL Server 复制的一部分发布。 说明 在升级过程中,SQL Server 利用存储在 syscomments 中的加密注释来重新创建加密过程。 

10.FOR REPLICATION :指定不能在订阅服务器上执行为复制创建的存储过程。.使用 FOR REPLICATION 选项创建的存储过程可用作存储过程筛选,且只能在复制过程中执行。本选项不能和 WITH RECOMPILE 选项一起使用。 

11.AS :指定过程要执行的操作。

12.sql_statement :过程中要包含的任意数目和类型的 Transact-SQL 语句。但有一些限制。

小结:看过这些基本语法后,下面我就根据语法创建各式的存储过程。


 创建存储过程


 

UserAccount
UserID UserName PassWord RegisterTime RegisterIP
12 6                    6                    2012-12-31 6
18 5                    5                    2013-01-01 5
19 1                    1                    2013-01-01 1
20 2                    2                    2013-01-01 2
21 3                    3                    2013-01-01 3
22 4                    4                    2013-01-01 4
23 5                    5                    2013-01-01 5
25 7                    7                    2013-01-01 7
26 8                    8                    2013-01-01 8
NULL NULL NULL NULL NULL

针对上面的表,我使用存储过程对它做一些操作:

1. 只返回单一记录集的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------
create Procedure GetUserAccount
as select * from UserAccount go -------------执行上面的存储过程---------------- exec GetUserAccount
copy code
copy code

 结果:相当于运行 select * from UserAccount 这行代码,结果为整个表的数据。

2.没有输入输出的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------

create Procedure inUserAccount
as insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9) go -------------执行上面的存储过程---------------- exec inUserAccount
copy code
copy code

 结果:相当于运行 insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(9,9,'2013-01-02',9) 这行代码。

3.有返回值的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------

create Procedure inUserAccountRe
as insert into UserAccount (UserName,[PassWord],RegisterTime,RegisterIP) values(10,10,'2013-01-02',10) return @@rowcount go -------------执行上面的存储过程---------------- exec inUserAccountRe
copy code
copy code

 解释:这里的@@rowcount为执行存储过程影响的行数,执行的结果是不仅插入了一条数据,还返回了一个值即 return value =1  ,这个可以在程序中获取,稍后在c#调用存储过程中会有说到。

4.有输入参数和输出参数的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------

create Procedure GetUserAccountRe
@UserName nchar(20), @UserID int output as if(@UserName>5) select @UserID=COUNT(*) from UserAccount where UserID>25 else set @UserID=1000 go -------------执行上面的存储过程---------------- exec GetUserAccountRe '7',null
copy code
copy code

 解释:@UserName为输入参数,@UserID为输出参数。 运行结果为@userID为COOUT(*)即 =1。

5. 同时具有返回值、输入参数、输出参数的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------

create Procedure GetUserAccountRe1
@UserName nchar(20), @UserID int output as if(@UserName>5) select @UserID=COUNT(*) from UserAccount where UserID>25 else set @UserID=1000 return @@rowcount go -------------执行上面的存储过程---------------- exec GetUserAccountRe1 '7',null
copy code
copy code

 结果:@userID为COOUT(*)即 =1,Retun Value=1。

6.同时返回参数和记录集的存储过程 

copy code
copy code
-------------创建名为GetUserAccount的存储过程----------------

create Procedure GetUserAccountRe2
@UserName nchar(20), @UserID int output as if(@UserName>5) select @UserID=COUNT(*) from UserAccount where UserID>25 else set @UserID=1000 select * from UserAccount return @@rowcount go -------------执行上面的存储过程---------------- exec GetUserAccountRe2 '7',null
copy code
copy code

 Result: Return the result set of executing  the code select  from  UserAccount, and @userID is COOUT(*) ie =1, Retun Value=9. 

7. A stored procedure that returns multiple recordsets 

copy code
copy code
-------------- Create a stored procedure named GetUserAccount----------------

create Procedure GetUserAccountRe3 as select * from UserAccount select * from UserAccount where UserID > 5 go -- -----------Execute the above stored procedure--------------- - exec GetUserAccountRe3
copy code
copy code

 Result: Two result sets are returned, one for  select  from  UserAccount and the other for  select  from UserAccount where UserID > 5 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904297&siteId=291194637