Database Functions
Overview
In this lesson, we will learn how to create a new function in SQL and associate this new object in Wayfast. If we have to repeatedly write large SQL scripts to perform the same task, we can create a function that performs that task.
Let’s review how we can simply call that function instead of rewriting scripts.
Create a Function in SQL and link to the Project’s DB
First we need to create an easy function in SQL to establish a currency format in our application. We can follow these recommendations to create the function:
Define the CREATE FUNCTION statement:
-Specify a name for the function.
-Specify a name and data type for each input parameter.
-Specify the RETURNS keyword and the data type of the scalar return value.
-Specify the BEGIN keyword to introduce the function-body.
-Specify the function body. Specify the RETURN clause and a scalar return value or variable.
-Specify the END keyword.CREATE FUNCTION fn_getMoneyFormat ( @variable money ) RETURNS varchar(max) AS BEGIN DECLARE @Value varchar(max) SELECT @Value = FORMAT(@variable,'#,###.00','EN-us') --Result RETURN @Value END
Execute the CREATE FUNCTION statement
Go to Wayfast and click on “End Task” button
A popup is displayed and there’s no need to change any requirement. We are going to synchronize Wayfast to get the new function available for development.
Automatically the new function is displayed on the “Functions” section
How to associate an existing Function in Wayfast?
In case we want to include functions previously generated in the new project, we can assign the elements selecting the specific database where they are allocated and include them for development.
Turn to “Database” at top menu and click on “Functions” tab
Click on “Associating Functions” button
A popup is displayed on screen. You can retrieve all the “function” objects from the specific instance and database selection. Click on “Assign” button
The new object is displayed on “Functions” dashboard.
All the functions are going to be listed in the popup and ready to be checked for assignation.
In case we need to review the “Function” script in Wayfast, click on “Edit Script” pencil icon and we can review the function associated to the project. Query Analyzer will be opened in another tab to verify the syntax
In case we have to edit the script, we can use “Synchronize” button to establish the connection to the database and update what we modified in Wayfast.
Recap
Wayfast brings multiple tools to develop our projects. In this lesson, we learnt how to create and edit function’s statements. This approach give us an easy wat to synchronize the objects between database and application selecting the correct instance.