SQL Server Series: stored procedures

Source: Internet
Author: User

The main purpose of customer/server development is to process dataProgramMove to the nearest possible place to the data. By moving data processing programs from customer applications to servers, you can reduce network traffic and improve performance and data integrity.

To move the data processing program closer to the data, one of the most common methods is to develop the stored procedure.

1. Manage stored procedures

1.1, create, alter, and drop

Stored procedures are managed by the following data definition language commands: Create, alter, and drop.

Create is used to create a stored procedure. It is the first command in batch processing. The normal completion of batch processing means that the stored procedure is successfully created. The drop command will delete stored procedures from the database, and the alter command will replace the original stored procedures with the newCode. If you want to modify a stored procedure, you 'd better use the alter method instead of deleting it first and then recreating it, because the latter method will lose all the permission settings in the original stored procedure.

 
Create ProcedureCategorylistAsSelectCategorynameFromCategory

1.2. Return record set

Stored Procedures can return record sets.

ExecCategorylist

1.3 compile the Stored Procedure

Compilation of corresponding stored procedures is automatically performed. During the first execution of the stored procedure, the stored procedure is compiled and the compilation results are saved in the memory. If the server is restarted, all compilation results of all stored procedures will be lost. They will be re-compiled only when they are called again.

SQL Server uses the master. DBO. syscacheobjects table to record compiled objects. To view compiled stored procedures, run the following query:

 Select   Case (C. SQL As   Char ( 35 )) As Storeprocedure, cacheobjtype, usecounts As   Count  From Master. DBO. syscacheobjects C  Join  Master. DBO. sysdatabases d  On C. dbid =  D. dbid  Where D. Name =   Db_name  ()  And Objtype =   '  Proc  '  Order  By Storeprocedure

2. transmit data to the stored procedure

SQL Server Stored Procedures can use many input and output parameters.

2.1 input parameters

If you want to transmit data to a stored procedure using input parameters, you can list the parameters to be added after the stored procedure name in the create procedure command. Each parameter must start with @. Once the input parameter is declared, it becomes a local variable in the stored procedure. Just like declaring local variables, you must use valid data types to define input parameters. When calling a stored procedure, you must provide values for these input variables (unless the input variable has a default value ).

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.