Results 1 to 6 of 6

Thread: Creating A CLR Function (SQL SERVER 2005)

Threaded View

  1. #1

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Creating A CLR Function (SQL SERVER 2005)

    Title - Creating a CLR Function – (Converting Numbers to Words)

    Description

    CLR or Common Language Runtime functions allow you as a developer to harness the power of .net inside SQL Server, and in the case of functions, actually in-line with your SQL.
    This means you can do things like expose .Nets regular expressions and string manipulation ability in your function.
    In this example I will take a C# Class that I have found on the internet, which converts Numbers to their Word representation, and alter it so that it fits inside the CLR Function, I will then show how to register and run the function within SQL Server.

    Requirements

    SQL SERVER 2005 & VS Studio 2005 or above


    Tutorial

    First you need to create a new project inside visual studio. Go to New | Project >> Database | SQL Server Project.

    Fig1.

    Name:  CreateCLRProject.png
Views: 9076
Size:  30.5 KB


    Once you have named & created your project right click on the project and select Add | User-Defined Function.

    Fig 2.

    Name:  AddCLRFunction.png
Views: 8723
Size:  30.0 KB

    This will add a class with a basic function shell to the project for you, and it will look something like this

    vb Code:
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. using System.Data.SqlTypes;
    5. using Microsoft.SqlServer.Server;
    6.  
    7. public partial class UserDefinedFunctions
    8. {
    9.     [Microsoft.SqlServer.Server.SqlFunction]
    10.     public static SqlString Function1()
    11.     {
    12.         // Put your code here
    13.         return new SqlString("Hello");
    14.     }
    15. };

    Above the function declaration you will notice this line -
    vb Code:
    1. [Microsoft.SqlServer.Server.SqlFunction]

    If you wish to read from the SQL Server tables inside your CLR function you will need to alter it to read;

    vb Code:
    1. [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]

    Now remove the current function outline so you have a blank class awaiting the import of code.

    vb Code:
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. using System.Data.SqlTypes;
    5. using Microsoft.SqlServer.Server;
    6.  
    7. public partial class UserDefinedFunctions
    8. {
    9. [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    10.    
    11. //Code Goes Here
    12.  
    13. };

    At this point you could essentially add in any supported code into your project that takes in a value and returns something.
    I was looking for some specific code to convert Numbers to Words, something that is very useful in Cheque processing for instance. I decided to use the code from the following site -

    http://www.codeproject.com/KB/cs/codesamples.aspx

    You will then need to import the code, and changed all the subs/functions to static, as they have to be static in a CLR Function and because logically they should be static anyway as a CLR Function is not object specific and returns a value based upon the Input, not upon an object.
    e.g.

    vb Code:
    1. public partial class UserDefinedFunctions
    2. {
    3. [Microsoft.SqlServer.Server.SqlFunction] (DataAccess = DataAccessKind.Read)]
    4.    
    5. public static SqlString convertNumericToWords(SqlDouble numb)
    6.         {
    7.             String num = numb.ToString();
    8.             return (changeToWords(num, false));
    9.         }
    10. };

    For the Public functions that are going to be registered with SQL Server you will also need to change the data types of the function itself and its input variables. The variables need to match up by Type with what it is being passed by SQL Server.

    There is a great link which tells you what datatypes map to which – Here

    The Datatypes SqlString & SqlDouble along with others all become available because you are in a .Net SQL Server Project, and their SQL Server equivalents are nVarchar and Float.
    Last edited by NeedSomeAnswers; Mar 2nd, 2010 at 04:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width