SQL Server Stored Procedure

Source: Internet
Author: User

The SQL Server Stored Procedure is a named set of transacation-SQL statements stored on the server. It is a method for encapsulating repetitive work, it supports declared variables, conditional execution, and other powerful programming functions.

SQL Server Stored Procedures have the following advantages over other database access methods:

(1) reuse. Stored procedures can be reused to reduce the workload of database developers.

(2) improve performance. The stored procedure is compiled when it is created. You do not need to re-compile the stored procedure in the future. A general SQL statement needs to be compiled every time it is executed, so the efficiency is improved by using the stored procedure.

(3) reduce network traffic. The stored procedure is stored on the server. You only need to pass the name and parameters of the stored procedure when calling the procedure. This reduces the amount of data transmitted over the network.

(4) Security. Parameterized stored procedures can prevent SQL injection attacks and apply Grant, deny, and revoke permissions to stored procedures.

SQL Server Stored Procedures are divided into three types: User-Defined stored procedures, extended stored procedures, and system stored procedures.

User-Defined stored procedures are classified into transaction-SQL and CLR.

1. The transaction-SQL stored procedure is a set of stored transaction-SQL statements that can accept and return user-supplied parameters.

2. CLR Stored Procedures refer to the reference to the. NET Framework Common Language Runtime (CLR) method, which can accept and return user-supplied parameters. They are in the. NET FrameworkProgramThe set is implemented as a public static method of the class. (This article will not be introduced)

The statement used to create the SQL server stored procedure is as follows:

Reference content is as follows:

 
 
  1. Create {proc | procedure} [Schema_name.] procedure_name [; number]
  2. [{@ Parameter [type_schema_name.] data_type}
  3. [Varying] [=Default] [[Out [put]
  4. ] [, N]
  5. [With <procedure_option> [, N]
  6. [For replication]
  7. As {<SQL _statement> [;] [N] | <method_specifier>}
  8. [;]
  9. <Procedure_option >::=
  10. [Encryption]
  11. [Recompile]
  12. [Execute_as_clause]
  13.  
  14. <SQL _statement >::=
  15. {[Begin] Statements [end]}
  16.  
  17. <Method_specifier >::=
  18. External name assembly_name.class_name.method_name
  19.  

[Schema_name]: name of the architecture to which the stored procedure belongs

For example:

 
  
  
  1. Create schema yangyang8848
  2. Go
  3. Create proc yangyang8848.allgoods
  4. As select * From master_goods
  5. Go

Execute: exec allgoods has an error.

Run exec yangyang8848.allgoods.

[; Number]: an optional integer used to group processes with the same name. You can use a drop procedure statement to delete these grouping processes together.

For example:

  
  
  1. Create proc S1; 1
  2. As
  3. Select * From master_goods
  4. Go
  5. Create proc S1; 2
  6. As
  7. Select * From master_location
  8. Go

Two stored procedures have been created. They are in the same group S1. If exec S1 is executed, the stored procedure defaults to Exec S1; 1. If we want to obtain information about all data points, execute exec S1; 2. To delete a stored procedure, only drop exec S1 can be executed, and all stored procedures in the group are deleted.

[@ Parameter]: A parameter in a stored procedure. You must assign a value to the parameter when calling the stored procedure unless the default value is set during Parameter definition or the parameter is set to equal to another parameter.

SQL Server Stored Procedures have a maximum of 2100 parameters.

For example:

  
  
  1. Create proc yangyang8848.onegoods
  2. @ Goodscode varchar (10)
  3. As
  4. Select * From master_goods where goodscode = @ goodscode
  5. Go

CalledCode:

 
  
  
  1. Declare @ code varchar (10)
  2. Set @ code ='123' 
  3. Exec yangyang8848.onegoods @ code

Adding output after the parameter indicates that this parameter is an output parameter.

  
  
  1. Create proc yangyang8848.onegoods
  2. @ Goodscode2 varchar (10) output, @ goodscode varchar (10) ='123' 
  3. As
  4. Select * From master_goods where goodscode = @ goodscode
  5. Set @ goodscode2 ='123' 
  6. Go

Call method:

  
  
  1. Declare @ vv2 varchar (10)
  2. Exec yangyang8848.onegoods @ codeOut 

NOTE: If one of the two parameters in the stored procedure has a default value and the other does not, we need to put the default value behind it. Otherwise, a problem may occur ~~

Careful friends may see some differences in the preceding statements. For example, the stored procedure uses output, while the calling statement uses out. I want to tell you that the two are the same.

[Recompile]: indicates that the database engine does not cache the plan of the process and the process is compiled at runtime. If for replication is specified, this option cannot be used. Recompile cannot be specified for CLR stored procedures.

This is a very useful function object_id: returns the database object ID of the objects in the schema range.

For example, when creating a stored procedure, you can write the following code:

 
 
  1. If object_id ('Angyang8848. onegoods') Is not null
  2. Drop proc yangyang8848.onegoods
  3. Go
  4.  
  5. Create proc yangyang8848.onegoods
  6. @ Goodscode2 varchar (10)Out, @ Goodscode varchar (10) ='123' 
  7. As
  8. Select * From master_goods where goodscode = @ goodscode
  9. Set @ goodscode2 ='123' 
  10. Go

For the stored procedure above, we call the following SQL query

  
  
  1. SelectDefinitionFromSYS. SQL _modules
  2. WhereObject_id = object_id ('Angyang8848. onegoods');

We can check the results.

However, if we add [encryption] to this stored procedure, you will not be able to see any results.

 

 
 
  1. If object_id ('Angyang8848. onegoods') Is not null
  2. Drop proc yangyang8848.onegoods
  3. Go
  4.  
  5. Create proc yangyang8848.onegoods
  6. @ Goodscode2 varchar (10)Out, @ Goodscode varchar (10) ='123' 
  7.  
  8. With Encryption
  9. As
  10. Select * From master_goods where goodscode = @ goodscode
  11. Set @ goodscode2 ='123' 
  12. Go

Then we query the SYS. SQL _modules directory view and return NULL to you.

Then run the following SQL statement: exec sp_helptext 'yangyang8848. onegoods'

You will get the following results: the text for object 'angyang8848. onegoods 'is encrypted.

Here you should understand that the [encryption] parameter is an encryption function that converts the original text of the create procedure statement to a fuzzy format. Fuzzy code output cannot be directly displayed in any directory view of SQL Server 2005. Users who do not have access to system tables or database files cannot retrieve fuzzy text. However, privileged users who can access system tables through the DAC port or who can directly access database files can use this text. In addition, users who can append a debugger to server processes can retrieve decrypted processes from the memory at runtime.

I wrote an introduction to cursors two days ago.ArticleBelow is an example to use the cursor with the stored procedure:

 
 
  1. If object_id ('Dbo. getmastergoods') Is not null
  2. Drop proc DBO. getmastergoods
  3. Go
  4.  
  5. Create proc getmastergoods
  6. @ Mycursor cursor varying output
  7. With Encryption
  8. As
  9. Set @ mycursor = cursor
  10. For
  11. Select goodscode, goodsname from master_goods
  12. Open @ mycursor
  13. Go

Create another stored procedure to traverse the cursor output result

 
 
  1. Create proc getallgoodsidandname
  2. As
  3.  
  4. Declare @ goodscode varchar (18)
  5. Declare @ goodsname nvarchar (20)
  6. Declare @ mastergoodscursor cursor
  7. Exec getmastergoods @ mastergoodscursorOut 
  8. Fetch next from @ mastergoodscursor
  9. Into @ goodscode, @ goodsname
  10. While (@ fetch_status = 0)
  11. Begin
  12. Begin
  13. Print @ goodscode +':'+ @ Goodsname
  14. End
  15. Fetch next from @ mastergoodscursor
  16. Into @ goodscode, @ goodsname
  17. End
  18. Close @ mastergoodscursor
  19. Deallocate @ mastergoodscursor
  20. Go

Finally, execute exec getallgoodsidandname and the result is as follows:

 
  
  
  1. 0003: Product 0003
  2. 0004: Product 0004
  3. 0005:123123
  4. 0006: Product 0006
  5. 0007: Product 0007
  6. 0008: Product 0008
  7. 0009: Product 0009
  8. 0010: Product 0010
  9. 0011: Product 0011
  10. 0012: Product 0012
  11. 0013: Product 0013
  12. 0014: Product 0014
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.