SQLCLR custom aggregate with multiple sql_variant parameters

后端 未结 2 514
野的像风
野的像风 2021-01-24 14:54

Hy,

I have post a question about CLR User-Defined Aggregates few month ago oon this post.

This works like a charm. But now I would like to quite the same functi

2条回答
  •  耶瑟儿~
    2021-01-24 15:34

    When using SQL_VARIANT / object, you can determine the type by using the GetType() method as follows:

    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = false, IsPrecise = true)]
    public static SqlBoolean GetIfSmallInt(object SomeValue)
    {
        return (SomeValue.GetType() == typeof(SqlInt16));
    }
    

    And test it using:

    DECLARE @DateTime DATETIME = GETDATE();
    SELECT dbo.GetIfSmallInt(@DateTime);
    -- 0
    
    DECLARE @SmallInt SMALLINT = 5;
    SELECT dbo.GetIfSmallInt(@SmallInt);
    -- 1
    

    Please keep in mind that using SQL_VARIANT / object has a noticeable performance penalty. Only use it when you absolutely need to. If you just need to pass in INT / SMALLINT / BIGINT, then use BIGINT / SqlInt64 as the input parameter type.

提交回复
热议问题