Create procedure
Create a stored procedure. The stored procedure is a set of statements that can receive 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]
AsSQL _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 canProcedure_nameAdd a identifier (#Procedure_nameTo create a global temporary process, you canProcedure_nameAdd two numbers (##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 orders can use a process namedOrderproc; 1,Orderproc; 2. Drop procedureOrderprocThe statement removes the entire group. If the name contains a bound identifier, the number should not be included in the identifier.Procedure_nameUse proper delimiters before and after.
@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 (includingText,NtextAndImage. However,CursorThe data type can only be used for output parameters. If the specified data type isCursorThe varying and output keywords must also be specified. For more information about the data types and syntax provided by SQL Server, see data types.
DescriptionForCursorThere is no limit on the maximum number of output parameters of the data type.
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.Text,NtextAndImageParameters 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 SQL server encryptionSyscommentsThe table contains entries in the create procedure statement text. Encryption prevents the process from being published as part of SQL Server replication.
DescriptionDuring the upgrade, SQL ServerSyscommentsTo 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.