|Encapsulate as Scalar Function|
The Encapsulate as Scalar Function command converts the selected code into a new scalar function and optionally replace it with a SELECT of the function return value.
This topic contains the following sections:
Statements creating or altering stored procedures, functions, triggers, views, rules and defaults cannot be encapsulated.
To encapsulate code as a new scalar function:
1. Select the script that you want to use for the new scalar function.
2. On the SQL Enlight->Refactor menu, click Encapsulate as Scalar Function... .
3. The Encapsulate as Scalar Function dialog box is displayed. The SQL script for the creation of the scalar function is displayed in the Preview box.
4. Type the name of the owner in the Owner edit box.
5. Type the name of the function in the Name edit box.
6. Modify the order and the type of the parameters using the Parameters grid.
The function name is not checked for duplicate names. If a function with this owner and name already exists, the script will fail.
7. Review the generated script in the Preview box and modify it if necessary.
8. Select Modify source script to reference the new object if you want SQL Enlight to modify the originating script and replace the encapsulated code with reference to the new function. The generated reference to the new scalar function is an SELECT statement selecting the return of the function call.
9. Click Apply button to apply the changes.
The parameters of the new objects are determined from the variables which are referenced in the selected code.
SQL Enlight automatically resolves the data type and the direction of these parameters. If a variable is used, but declared nowhere in the script, its declaration in the resulting script will have sql_variant data type.