IT tutorials
 
Database
 

SQL Server 2008 R2 : Creating and Managing User-Defined Functions (part 3) - Creating User-Defined Functions - Creating Custom Function Templates

1/29/2013 4:20:48 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Creating Custom Function Templates

To create a custom function template, right-click the Function folder in the Template Explorer and select New. SSMS then creates an entry in the Template Explorer, and you can specify the name for the template, as shown in Figure 4.

Figure 4. Creating a new function template in SSMS.


To begin adding code to the template, you double-click it or right-click and select Open. A blank query window appears, and you can use it to enter the new template code. Probably the best way to get started is to copy the template code from one of the templates provided with SQL Server 2008.

Listing 2 shows an example of a new function template.

Listing 2. An Example of Custom Function Template
--=========================================
-- SQL Server 2008 Unleashed Sample
--  Create scalar-valued function template
--=========================================

USE <database_name, sysname, bigpubs2008>
GO

IF OBJECT_ID (N'<schema_nm, sysname, dbo>.<func_nm, sysname, fn_myfunc>')
      IS NOT NULL
   DROP FUNCTION <schema_nm, sysname, dbo>.<func_nm, sysname, fn_myfunc>
GO

CREATE FUNCTION <schema_nm, sysname, dbo>.<func_nm, sysname, fn_myfunc>
    (<parameter1, sysname, @param1> <parameter1_datatype,, int>,
     <parameter2, sysname, @param2> <parameter2_datatype,, int>,
     <parameter3, sysname, @param3> <parameter3_datatype,, int>)
RETURNS <return_value_datatype,,int>
WITH EXECUTE AS CALLER
AS
-- place the body of the function here
BEGIN
    DECLARE <variable1, sysname, @var1> <variable1_datatype,, int>,
            <variable2, sysname, @var2> <variable2_datatype,, int>

    select <variable1, sysname, @var1> = isnull(<parameter1, sysname, @param1> )
    <T-SQL_Body,,>

    RETURN <variable1, sysname, @var1>
END
 GO


					  

After you define a custom function template, you can use it as you do the built-in templates. You can double-click it or right-click and select Open, and SSMS opens a new query window with a new function creation script based on the custom template. If you use the default values for the template parameters, after the parameter substitution, your CREATE FUNCTION script should look like the one in Listing 3.

Listing 3. An Example of a CREATE FUNCTION Script Generated from a Custom Function Template
--=========================================
-- SQL Server 2008 Unleashed Sample
--  Create scalar-valued function template
--=========================================

USE bigpubs2008
GO

IF OBJECT_ID (N'dbo.fn_myfunction') IS NOT NULL
   DROP FUNCTION dbo.fn_myfunction
GO

CREATE FUNCTION dbo.fn_myfunction
    (@param1 int,
     @param2 int,
     @param3 int)
RETURNS int
WITH EXECUTE AS CALLER
AS
-- place the body of the function here
BEGIN
    DECLARE @var1 int,
            @var2 int
    select @var1 = isnull(@param1 )


    RETURN @var1
END
 GO		  

 
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