Encapsulate as Scalar Function
Introduction
Section titled “Introduction”The Encapsulate as Scalar Functioncommand converts the selected code into a new scalar function and optionally replaces it with a SELECT of the function return value.
⚡ ** note: ** Statements creating or altering stored procedures, functions, triggers, views, rules, and defaults cannot be encapsulated.
To encapsulate code as a new scalar function:
-
Select the script that you want to use for the new scalar function.
-
On the SQL Enlight->Refactormenu, click Encapsulate as Scalar Function….
-
The Encapsulate as Scalar Functiondialog box is displayed. The SQL script for the creation of the scalar function is displayed in the Previewbox.

-
Type the name of the owner in the Owner edit box.
-
Type the name of the function in the Name edit box.
-
Modify the order and the type of the parameters using the Parameters grid.
⚡ ** note: ** The function name is not checked for duplicate names. If a function with this owner and name already exists, the script will fail.
-
Review the generated script in the Previewbox and modify it if necessary.
-
Select Modify source script to reference the new objectif you want SQL Enlightto modify the originating script and replace the encapsulated code with reference to the new function. The generated reference to the new scalar function is a SELECT statement selecting the return of the function call.
-
Click Applybutton to apply the changes.
Parameters
Section titled “Parameters”The parameters of the new objects are determined from the variables which are referenced in the selected code.
SQL Enlightautomatically 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_variantdata type.