How can one create a custom aggregation function for MS SQL with variable number of arguments?
public void Accumulate(SqlString value, params SqlInt32 [] ids)
{
// check for compatibility with the previous ids and sum if compatible
}
When I deploy the assembly, I get the following error:
SqlClrDeploy:
Beginning deployment of assembly My.SQLExtensions.dll to server srvname : dbname
The following error might appear if you deploy a SQL CLR project that was built
for a version of the .NET Framework that is incompatible with the target
instance of SQL Server: "Deploy error SQL01268: CREATE ASSEMBLY for assembly
failed because assembly failed verification". To resolve this issue,
open the properties for the project, and change the .NET Framework version.
path\to\assembly.dll : Deploy error : Could not create a type for parameter
System.Data.SqlTypes.SqlInt32[] ids
If it is not possible to use custom aggregations with varargs, what would be a workround?
The purpose of this custom aggregation is to calculate totals, if all the ids
remain the same in the group (it returns null, if any of ids changes).
Update: Finally I chose to do it in SQL, without custom aggregation. For example:
SELECT a, b, c,
CASE WHEN COUNT(*) = 1 OR
COUNT(id) = 0 OR
(VAR(id) = 0 AND (COUNT(*) - COUNT(id) = 0))
THEN SUM(value)
ELSE NULL END
FROM TABLE GROUP BY primaryID