Stored Procedures, functions-default parameter values

Source: Internet
Author: User

http://topic.csdn.net/u/20100710/10/b3fbaef9-af39-454a-9c34-b48d207532a8.html

-- first look at the execution of several processes

-- first, create a general statement

Create proc P ( @ s varchar ( 1 ) = ' A ' )

As

Select @ S

Go

ExecP

Go
-- 1
Alter Proc P ( @ S Decimal = A)
As
Select @ S
Go
Exec P
Go
/*

Server: Message 8114, level 16, status 5, process P, row 0
An error occurred while converting nvarchar to numeric.

*/

-- 2
Alter Proc P ( @ S Varchar ( 1 ) = Medium)
As
Select @ S
Go
Exec P
Go
/*


----

(The number of affected rows is 1)

*/
-- 4
Alter Proc P ( @ S Nvarchar ( 1 ) = Medium)
As
Select @ S
Go
Exec P
Go
/*
----
Medium

(The number of affected rows is 1)

*/
--Data Type priority http://msdn.microsoft.com/zh-cn/library/ms190309 (SQL .90). aspx
--
1. Some character types can be passed by the default value of the parameter,
--
2. During the parameter conversion process, the default value will first convert the data type specified by the parameter; otherwise, it will be converted to the highest level of this type.

-- 5
Alter Proc P ( @ S Varchar ( 10 ) = 1AA)
As
Select @ S
Go
Exec P
Go
/*
Server: Message 170, Level 15, status 1, process P, Row 1
Row 3: There is a syntax error near 'A.
*/

-- 6
Alter Proc P ( @ S Varchar ( 10 ) = A)
As
Select @ S
Go
Exec P
Go
/*
Server: Message 170, Level 15, status 1, process P, Row 1
Row 1st: There is a syntax error near 'A.
*/

-- For more information, see the preceding URL.
--
/* Default value of the specified parameter
--
You can create a stored procedure with optional parameters by specifying the default value for optional parameters. When executing this stored procedure, if other values are not specified, the default value is used.
--

--
If the default value of the parameter is not specified in the stored procedureProgramIf the parameter is not provided when the stored procedure is executed, a system error is returned. Therefore, it is necessary to specify the default value.
--

--
If you cannot specify an appropriate default value for a parameter, you can specify NULL as the default value of the parameter and run the stored procedure without providing the parameter value, make the Stored Procedure return a custom message.
--

--
Note:
--
If the default value is a string that contains embedded spaces or punctuation marks, or starts with a number (for example, 6xxx), the default value must be caused by a straight single quotation mark. */

-- -I wrote a blog for the first time without any technical skills. You are welcome to give instructions. The default parameters of the function are similar. You can try it.

-- Original post http://topic.csdn.net/u/20100709/22/4ca8d993-aa1a-4157-b2c3-7227006d8b6b.html? Seed = 1368739759 & R = 66863373 # r_66863373

-- Since the default values of stored procedure parameters are involved, functions are indispensable.

-- What is the difference between the default value, type, and return type of a function and stored procedure parameter?
--
1. There are some differences during execution. stored procedures can omit parameters with default values, but functions won't work. You must specify the default keyword instead.
--
2 parameter type, return type

-- Refer to books online

-- Stored Procedure
/*

-- Maximum parameter limit,
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.

-- Parameter 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.

-- Output type and default

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.
*/

*/

--Function
/*

-- Maximum parameter limit and default value
User-Defined function parameters. One or more parameters can be declared in the create function statement. A function can have a maximum of 1,024 parameters.
During function execution, the value of each declared parameter must be specified by the user, unless the default value of this parameter has been defined. If function parameters have default values,
You must specify the "default" keyword when calling this function to obtain the default value. This behavior is different from the parameter with default values in the stored procedure. Omitting a parameter in the stored procedure also means that the default value is used.

Use the @ symbol as the first character to specify the parameter name. The parameter name must comply with the identifier rules. Each function parameter is only used for this function;
The same parameter name can be used in other functions. Parameters can only replace constants, but cannot replace the names of table names, column names, or other database objects.

-- Parameter Data Type

The data type of the parameter. All scalar data types (including bigint and SQL _variant) can be used as parameters for user-defined functions.
The timestamp data type and user-defined data type are not supported.
You cannot specify non-standard data types (such as cursor and table ).

-- Return type

Is the return value of a scalar user-defined function. Scalar_return_data_type
It can be any scalar data type supported by SQL Server (except text, ntext, image, and timestamp ).

*/

-- Summary

-- Parameter type
--
Stored Procedures can use cursor as the parameter type, but they are output parameters without the maximum limit,
--
The data type of the parameter. All data types (including text, ntext, and image) can be used as parameters for stored procedures. Maximum 2100
--
The data type of the parameter. All scalar data types (including bigint and SQL _variant) can be used as parameters for user-defined functions. Maximum 1024

-- Default Value
--
Default Value of the stored procedure. You do not need to specify the default keyword during execution.

-- Return type
--
Function
--
It can be any scalar data type supported by SQL Server (except text, ntext, image, and timestamp ).
--
Process
--
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.

--The solution to converting getdate () to the default function value is to create a getdate view and pass B as the parameter. You are welcome to provide a new method for these two methods.

--The stored procedure can dynamically execute exec statements, which is equivalent to passing in the table name as a parameter, while functions do not support dynamic execution ;;;;

--For the above content, refer to the online series and msdn. The author's information is not provided for reprinting. 

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.