Understanding stored procedures in SQL Server

Source: Internet
Author: User

The stored procedure of the database is understood as, the subroutine that processes the data, writes like function, uses up like function, in SQL Server the stored procedure divides into two big classes, the system and the custom, the system all puts in the Master System database, the custom is writes itself, uses the DDL the related statement to be able, Creating a stored procedure, like a function, can have a parameter, or it can be without a parameter, and it is simple to write without a reference:

CREATE PROC Hello  as    BEGIN        /*.... *    /ENDGO

When executing, you just need to execute it in your custom stored procedure.

EXEC Hello

When you have a reference, you are not a stranger:

CREATE PROC @n1 int  as    BEGIN        // .....     END GO EXEC ' 123 ' EXEC @n2 = ' 123 ' * *

If the first statement of the batch is executed, the exec can be omitted

At the time of writing, there will be many different requirements, the stored procedure does not return the value, we need to return the relevant information when we want to be similar to the same-C + + to pass the pointer or reference, in SQL Server is implemented by adding output after the variable.

There are a number of properties that are decorated with parameters:

VARYING Applies only to the cursor variable for the output parameter, which is dynamically constructed by the stored procedure
Parameter = value Represents the default value
READONLY Modifying parameters that cannot be updated or modified in a stored procedure, similar to the const
RECOMPILE Recompile when executing stored procedures

Precautions:

Custom stored procedures can only be created in the current database except temporary stored procedures, temporary stored procedures are created in the system database tempdb

After the stored procedure is created successfully, the procedure name is in sysobjects and the statement text is stored in syscomment

Automatic execution of stored procedures is a number of stored procedures that SQL Server automatically executes during startup, and these stored procedures cannot have parameters.

Understanding stored procedures in SQL Server

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.