SQLCLR custom aggregate with multiple parameters

前端 未结 2 451
长情又很酷
长情又很酷 2021-01-29 00:09

I have trouble understanding of how CLR User-Defined Aggregates work.

I have to create some custom CLR aggregates with multiple parameters. The point is to get the value

2条回答
  •  孤独总比滥情好
    2021-01-29 00:53

    If you are looking for an implementation of your specific request, then @Richard's answer looks to be correct (though, you might still need to implement the Read and Write methods for using a custom type -- Format.UserDefined).

    However, it seems from the comments on the question that this is more of a general question of when to do processing of whatever information you are collecting. In that case:

    • The Accumulate method is called for every row in a particular GROUP. This is the entry point.

    • The Merge method is called when parallelism is being used. SQL Server uses this method to combine the information from various threads. Depending on the type of algorithm you are doing, here you might: combine the current and incoming information, decide to keep the current info or the incoming info (as is being done in @Richard's implementation), recalculate the current info based on the new incoming info.

    • The Terminate method is called at the end of each particular GROUP. Here is where you would do the final calculation / logic and then return the expected result.

    This information, and more, can be found on the MSDN page for Requirements for CLR User-Defined Aggregates.

提交回复
热议问题