8

While writing a CLR function Sql Server can we use namespaces ?

namespace SomeName1.SomeName2
{
   public static class SomeClass
   {
       [SqlFunction]
       public static SqlString SomeMethod(SqlString input)
       {
          // ....
       }
   }
}

If so, then how do we call this function from SqlServer. In other words how do we call CLR functions from SQL Server with namespaces?

Debjit
  • 410
  • 5
  • 16

1 Answers1

7

Yes, you absolutely can:

CREATE FUNCTION SomeMethod(@input VarChar(200))
RETURNS VarChar(200) WITH EXECUTE AS CALLER AS

EXTERNAL NAME [SomeName1.SomeName2].[SomeName1.SomeName2.SomeClass.SomeMethod]

Where [SomeName1.SomeName2] in the first part is the assembly as named in SQL Server, and the rest ([SomeName1.SomeName2.SomeClass.SomeMethod]) is the fully qualified function name, including the namespace.

Incidentally, if you deploy from Visual Studio it handles a lot of this for you.

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • 2
    I just figured that out. I was about to answer that. [AssemblyName][Namespace.Class].[Method]. Thanks ! – Debjit Jul 27 '11 at 19:05
  • 1
    It would actually be [AssemlyName] then the rest goes as [SomeName1.SomeName2.SomeClass].[SomeMethod] – Debjit Jul 27 '11 at 19:11
  • Correct. I'm assuming your assembly name follows that of the namespace being defined here. – Yuck Jul 27 '11 at 19:14
  • @Debjit - just a typo correction to your comment: [AssemblyName].[Namespace.Class].[Method]. Thanks! – Colin Jan 23 '17 at 22:10