- Local time
- Tomorrow, 07:38
- Joined
- Jan 20, 2009
- Messages
- 12,895
Today I opened a door in SQL Server and discovered a whole extra wing of machinery, particularly useful for Access VBA developers using SQL Server back ends. Turns out it is quite easy to port VBA code into SQL Server functions and gain a whole lot of performance improvement on the way.
CLR functions are imported to SQL Server databases from compiled dynamic link libraries (.dll) and work like any other inline function in TSQL. They can be very efficient because they are compiled executable code.
It is quite easy to make a dll in Visual Studio using VB.NET. For an experienced VBA developer, the learning curve to VB.NET is not particularly steep. Simple VBA code generally compiles when pasted into Visual Studio. The Intellisense literally tells you what to do to fix it if there are problems. One click on a menu and the dll is created.
From there, one line of SQL Creates an Assembly from the dll after which the dll file is no longer required. Another SQL command creates the CLR function from the Assembly.
In Visual Studio, start a Class Library project, paste your VBA function into the Class and include the word "Shared" into the declaration. The Intellisense is so good in Visual Studio that you can type in anywhere in the function declaration it will jump into the right place.
To be Shared, the function must be self contained, it cannot call another function in the class. Visual Studio will tell you this if your function includes outside calls.
You have to turn on CLR capabilities in SQL Server. (It will tell you this if it is off when you go to use the function.)
The trickiest part is the function declaration in SQL which must include the namespace property from the dll in the reference to the function's class. MSSQLSMS does not tell you this one. The namespace can be found in the Application tab of the Project Properties in Visual Studio.
Obviously the datatypes of the VB function must match the function declaration in SQL Server. Most of them are obvious if you are familiar with SQL datatypes. The SQL equivalent for a VB.NET string is nvarchar(max).
I Googled my way through with the help of the Intellisense and had my first VBA function running in SQL Server in a couple of hours. It's magic. Post back if you get stuck.
I will soon know more. I have a lot more VBA code I will be porting into SQL Server starting tomorrow morning.
CLR functions are imported to SQL Server databases from compiled dynamic link libraries (.dll) and work like any other inline function in TSQL. They can be very efficient because they are compiled executable code.
It is quite easy to make a dll in Visual Studio using VB.NET. For an experienced VBA developer, the learning curve to VB.NET is not particularly steep. Simple VBA code generally compiles when pasted into Visual Studio. The Intellisense literally tells you what to do to fix it if there are problems. One click on a menu and the dll is created.
From there, one line of SQL Creates an Assembly from the dll after which the dll file is no longer required. Another SQL command creates the CLR function from the Assembly.
In Visual Studio, start a Class Library project, paste your VBA function into the Class and include the word "Shared" into the declaration. The Intellisense is so good in Visual Studio that you can type in anywhere in the function declaration it will jump into the right place.

To be Shared, the function must be self contained, it cannot call another function in the class. Visual Studio will tell you this if your function includes outside calls.
You have to turn on CLR capabilities in SQL Server. (It will tell you this if it is off when you go to use the function.)
The trickiest part is the function declaration in SQL which must include the namespace property from the dll in the reference to the function's class. MSSQLSMS does not tell you this one. The namespace can be found in the Application tab of the Project Properties in Visual Studio.
Obviously the datatypes of the VB function must match the function declaration in SQL Server. Most of them are obvious if you are familiar with SQL datatypes. The SQL equivalent for a VB.NET string is nvarchar(max).
I Googled my way through with the help of the Intellisense and had my first VBA function running in SQL Server in a couple of hours. It's magic. Post back if you get stuck.
I will soon know more. I have a lot more VBA code I will be porting into SQL Server starting tomorrow morning.