IT tutorials
 
Database
 

SQL Server 2008 R2 : Creating and Managing User-Defined Functions (part 2) - Creating User-Defined Functions - Using SSMS to Create Functions

1/29/2013 4:19:35 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Using SSMS to Create Functions

To create a function by using SSMS, open the Object Explorer to the database in which you want to create the function. Then select the Programmability node, right-click the Functions node, select New, and then choose one of the three available options as shown in Figure 1:

  • Inline Table-Valued Function

  • Multistatement Table-Valued Function

  • Scalar-Valued Function

Figure 1. Creating a new function from the Object Browser in SSMS.

SSMS opens a new query window populated with a template for that type of function. Listing 1 shows an example of the default template code for an inline table-valued function that would be opened into a new query window.

Listing 1. An Example of a New Function Creation Script Generated by SSMS
-- ================================================
-- Template generated from Template Explorer using:
-- Create Inline Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:          <Author,,Name>
-- Create date: <Create Date,,>
-- Description:     <Description,,>
-- =============================================
CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName>
(
     -- Add the parameters for the function here
     <@param1, sysname, @p1> <Data_Type_For_Param1, , int>,
     <@param2, sysname, @p2> <Data_Type_For_Param2, , char>
)
RETURNS TABLE
AS
RETURN
(
     -- Add the SELECT statement with parameter references here
     SELECT 0
)


GO


					  

You can modify the template code as necessary to name the function and to specify the parameters, return value, and function body. When you are finished, you execute the contents of the query window to create the function. After you create a function successfully, you should save the source code to a file by choosing File, Save or File, Save As. This way, you can re-create the function from the file if it is accidentally dropped from the database.

One thing you might notice about the function templates is that they contain template parameters for parameter names and function names, for example. These template parameters are in the format <parameter_name, data_type, value>:

  • parameter_name is the name of the template parameter in the script.

  • data_type is the optional data type of the template parameter.

  • value is the default value to be used to replace every occurrence of the template parameter in the script.

You can automatically substitute values for template parameters by selecting Query, Specify Values for Template Parameters or by pressing Ctrl+Shift+M. The Specify Values for Template Parameters dialog, shown in Figure 2, appears.

Figure 2. Using the Specify Values for Template Parameters dialog with functions in SSMS.


Enter the values for the template parameters in the Value column and then click OK. SSMS then substitutes any values you specified wherever the template parameter is defined within the template.

An alternative way to create a function from a template is to use the Template Explorer in SSMS. You can open the Template Explorer by selecting View, Template Explorer in SSMS (see Figure 3) or by pressing Ctrl+Alt+T. The Template Explorer window appears in SSMS (which is also shown in Figure 3).

Figure 3. Opening the Template Explorer to create functions in SSMS.

You can double-click the template for the type of function you want to create or right-click the desired template and then select Open. SSMS opens a new query window populated with the template code.

Note

You are also able to edit the provided function templates available in the Template Explorer by right-clicking them and selecting Edit. You can then customize the templates to include code fragments, comments, or a structure that is more to your preferences and save the changes to the template file. However, it is generally recommended that you not modify the provided templates alone and instead create your own custom templates.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us