SQL Server procedure

Source: Internet
Author: User

This article content from Baidu Encyclopedia: http://baike.baidu.com/view/1031973.htm

Create procedure creates a stored procedure, which is a set of stored statements that can accept and return user-supplied parameters. You can create a process for permanent use, or temporary use in a session (Partial temporary process), or temporary use in all sessions (Global temporary process ). You can also create a stored procedure that runs automatically when Microsoft SQL Server is started.

Syntax

Create proc [edure] procedure_name [; number]

[{@ Parameter data_type}

[Varying] [= default] [Output]

] [,... N]

[

{Recompile | encryption | recompile, encryption}]

[For replication]

As SQL _statement [... n]

Parameters

Procedure_name

Name of the new stored procedure. The process name must comply with the identifier rules and must be unique to the database and its owner. For more information, see use identifiers.

To create a local temporary process, you can add a identifier (# procedure_name) before procedure_name. To create a global temporary process, you can add two identifier (# procedure_name) before procedure_name ). The complete name (including # Or #) cannot exceed 128 characters. The name of the specified process owner is optional.

Number

Is an optional integer used to group processes with the same name, so that the same group of processes can be removed with a drop procedure statement. For example, an application named ordersProgramThe process can be named orderproc; 1, orderproc; 2. The drop procedure orderproc statement removes the entire group. If the name contains a bound identifier, the number should not be included in the identifier and the appropriate delimiters should be used before and after procedure_name.

@ Parameter

Parameters in the process. One or more parameters can be declared in the create procedure statement. You must provide the value of each declared parameter during execution (unless the default value of this parameter is defined ). A stored procedure can have a maximum of 2.100 parameters.

Use the @ symbol as the first character to specify the parameter name. The parameter name must comply with the identifier rules. Each process parameter is only used for this process. The same parameter name can be used in other processes. By default, parameters can only replace constants, but cannot replace the names of table names, column names, or other database objects. For more information, see execute.

Data_type

The data type of the parameter. All data types (including text, ntext, and image) can be used as parameters for stored procedures. 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 and syntax provided by SQL Server, see data types.

It indicates that there is no limit on the maximum number of output parameters that can be cursor data types.

Varying

Specify the result set supported by the output parameter (dynamically constructed by the stored procedure, and the content can be changed ). Only applicable to cursor parameters.

Default

The default value of the parameter. If the default value is defined, the process can be executed without specifying the value of this parameter. The default value must be a constant or null. If the like keyword is used for this parameter, the default value can contain wildcards (%, _, [], and [^]).

Output

Indicates that the parameter is a return parameter. The value of this option can be returned to Exec [ute]. The output parameter can be used to return information to the call process. The text, ntext, and image parameters can be used as output parameters. The output parameter using the output keyword can be a placeholder cursor.

N

It indicates that up to 2.100 parameter placeholders can be specified.

{Recompile | encryption | recompile, encryption}

Recompile indicates that SQL server does not cache the plan of the process, and the process will be re-compiled at runtime. Use the recompile option when you use an atypical or temporary value instead of overwriting the execution plan cached in the memory.

Encryption indicates the entries in the SQL Server encrypted syscomments table that contain the create procedure statement text. Encryption prevents the process from being published as part of SQL Server replication.

It indicates that during the upgrade, SQL server uses the encryption annotation stored in syscomments to recreate the encryption process.

For Replication

The storage process created for replication cannot be executed on the subscription server .. Stored Procedures created using the for replication option can be used for filtering stored procedures and can only be executed during replication. This option cannot be used with the with recompile option.

As

Specifies the operation to be performed in the process.

SQL _statement

Any number and types of transact-SQL statements to be included in the process. But there are some restrictions.

N

Yes indicates that this process can contain placeholders for multiple Transact-SQL statements.

Note

The maximum size of stored procedures is 128 MB.

User-Defined stored procedures can only be created in the current database (except for temporary procedures, temporary procedures are always created in tempdb ). In a single batch, the create procedure statement cannot be combined with other Transact-SQL statements.

By default, the parameter can be blank. To prevent passing a null parameter value to a column that does not allow null, add the programming logic to the process or use the default value for the column (use the default keyword of create or alter table ).

Whether SQL Server interprets a null string as a single space or as a real Null String, which is controlled by compatibility level settings. If the compatibility level is less than or equal to 65, SQL Server interprets the Null String as a single space. If the compatibility level is 70, SQL Server interprets the Null String as a null string. For more information, see sp_dbcmptlevel.

Obtain information about stored procedures

To display the text used to create a process, execute sp_helptext in the database where the process is located and use the process name as a parameter.

The stored procedure created using the encryption option cannot be viewed using sp_helptext.

To display reports on objects referenced by a process, use sp_depends.

To rename a process, use sp_rename.

Reference object

The stored procedure that SQL Server allows to reference an object that does not exist. During creation, only the syntax check is performed. If there is no effective plan in the cache during execution, compile the stored procedure to generate the execution plan. All objects referenced in the stored procedure are resolved only during compilation. Therefore, if a stored procedure with the correct syntax references a nonexistent object, it can still be created successfully, but it will fail during runtime because the referenced object does not exist.

Execute the Stored Procedure

After the create procedure statement is successfully executed, the process name is stored in the sysobjects system table, and the text of the create procedure statement is stored in syscomments. During the first execution, the process is compiled to determine the optimal data access plan.

Cursor data type parameters

Stored procedures can only use the cursor data type for output parameters. If you specify the cursor data type for a parameter, you must also specify the varying and output parameters. If the varying keyword is specified for a parameter, the data type must be cursor and the output keyword must be specified.

 

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.