We have a third-party DLL that can work with the DataTable source information and generate some useful values, and we are trying to connect it through SQLCLR to be called as a table UDF in SQL Server 2008.
Taking the concept here is another step, I would like to program the CLR Table-Valued Function , which works with the source data table from the database.
I am sure I understand what should happen on the T-SQL side; but, what should the method signature look like in .NET code (C #)? What will be the parameter data type for "table data from SQL Server?"
eg.
CREATE TYPE InTableType AS TABLE (LocationName VARCHAR(50), Lat FLOAT, Lon FLOAT) GO CREATE TYPE OutTableType AS TABLE (LocationName VARCHAR(50), NeighborName VARCHAR(50), Distance FLOAT) GO CREATE ASSEMBLY myCLRAssembly FROM 'D:\assemblies\myCLR_UDFs.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS GO CREATE FUNCTION GetDistances(@locations InTableType) RETURNS OutTableType AS EXTERNAL NAME myCLRAssembly.GeoDistance.SQLCLRInitMethod GO DECLARE @myTable InTableType INSERT INTO @myTable(LocationName, Lat, Lon) VALUES('aaa', -50.0, -20.0) INSERT INTO @myTable(LocationName, Lat, Lon) VALUES('bbb', -20.0, -50.0) SELECT * FROM @myTable DECLARE @myResult OutTableType INSERT INTO @myResult GetDistances @myTable
The lat / lon â distance object is a stupid example, which, of course, should be best handled in SQL; but I hope this illustrates the general purpose of table-in-> table-out through a table-valued UDF bound to the SQLCLR assembly.
I am not sure if this is possible; What will be the signature signature of SQLCLRInitMethod in C #?
public class GeoDistance { [SqlFunction(FillRowMethodName = "FillRow")] public static IEnumerable SQLCLRInitMethod(<appropriateType> myInputData) {
If this is not possible, I know that I can use the "context connection = true" SQL connection in C # code to have the CLR component query for the necessary data , taking into account the corresponding keys; but it is sensitive to changes in the database schema. Thus, I hope that just SQL will fill in all the source data and pass it to the functions.
Bonus question - if it works at all, will it work with the table more than ?
c # sql-server-2008 sqlclr user-defined-functions
Skeolan
source share