I'm using a CLR table-valued function to SELECT and return the results of a complex database search which uses many variables.
The documentation shows that you build such a function in a fashion similar to this:
public partial class UserDefinedFunctions
{
private class ResultRow
// This class holds a row which we want to return.
{
public SqlInt32 CustId;
public SqlString Name;
public ResultRow(SqlInt32 custId_, SqlString name_)
{
CustId = custId_;
Name = name_;
}
}
[SqlFunction(
DataAccess = DataAccessKind.Read,
FillRowMethodName = "Test_FillRow",
TableDefinition = "CustId int" +
"Name nvarchar(50)")]
public static IEnumerable Test()
// This function contains the actual logic.
{
ArrayList results = new ArrayList();
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
using (SqlCommand select = new SqlCommand(
"SELECT TOP 100 custid, name FROM Customers",
connection))
{
using (SqlDataReader reader = select.ExecuteReader())
{
while (reader.Read())
{
results.Add(new ResultRow(
reader.GetSqlInt32(0), // CustId
reader.GetSqlString(1) // Name
));
}
}
}
}
return results;
}
public static void Test_FillRow(
object resultsObj,
out SqlInt32 custid,
out SqlString name)
// This function takes a row and tells SQL Server what variables we want to
// return from it and what types it contains.
{
ResultRow selectResults = (ResultRow)resultsObj;
custid = selectResults.CustId;
name = selectResults.Name;
}
}
The problem is, it's rather repetitive. First you define the table in the SqlFunction block. Then as you add or remove columns in the results you're returning, you have to make sure that you update it and
- the definition in ResultRow
- the arguments to the constructor in ResultRow
- the assignment in ResultRow
- the types grabbed from the reader in Test()
- the out arguments in Test_FillRow()
- the assignments in Test_FillRow()
- and the SQL query itself, which is the only part you're really trying to think about to start with.
I'm working on such a function which takes over twenty arguments, returns even more rows, and contains these eight possible places to make mistakes. Eight. All the mistakes are trivial and easily fixed, but it's very easy to make them because there's so many places in the code which I have to manually keep in sync.
This is a violation of DRY, but I don't know how to eliminate the repetition. Is there a more concise way to write CLR table-valued functions?