MS SQL用户定义的聚合函数中的可变参数个数

时间:2012-01-18 14:09:32

标签: sql-server aggregate-functions

如何为具有可变数量参数的MS SQL创建自定义聚合函数?

public void Accumulate(SqlString value, params SqlInt32 [] ids)
{
    // check for compatibility with the previous ids and sum if compatible
}

部署程序集时,出现以下错误:

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

如果无法将自定义聚合与varargs一起使用,那么什么是workround?

此自定义聚合的目的是计算总计,如果组中的所有ids保持不变(如果任何ID发生更改,则返回null)。


更新:最后,我选择在SQL中执行此操作,而不进行自定义聚合。例如:

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

0 个答案:

没有答案