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:
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.
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).
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.