SQL2005CLR函数扩展-深入环比计算的详解(sql环比怎么算)学到了吗

随心笔谈12个月前发布 admin
81 0

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions

{

    // 保存当前组当前值

    private static System.Collections.Generic.Dictionary _listValue=new System.Collections.Generic.Dictionary ();

    // 保存当前组

    private static System.Collections.Generic.Dictionary _listGroup =new System.Collections.Generic.Dictionary ();

    ///

    /// 获取当前组上条记录数值

    ///

    /// 并发键

    /// 当前组

    /// 当前组当前值

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlString GetPrevMemberValue(SqlString key,SqlString currentGroup,SqlString currentValue)

    {

        if (key.IsNull || currentGroup.IsNull) return SqlString .Null;

        try

        {

            SqlString prevMemberValue=_listValue[key.Value];

            // 组变更

            if (_listGroup[key.Value] !=currentGroup.Value)

            {

                prevMemberValue=SqlString .Null;

                _listGroup[key.Value]=currentGroup.Value;

             }

            // 值变更

            _listValue[key.Value]=currentValue;

            return prevMemberValue;

        }

        catch

        {

            return SqlString .Null;

        }

    }

    ///

    /// 初始化并发键

    ///

    ///

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlBoolean InitKey(SqlString key)

    {

        try

        {

            _listValue.Add(key.Value, SqlString .Null);

            _listGroup.Add(key.Value, string .Empty);

            return true ;

        }

        catch

        {

            return false ;

        }

    }

    ///

    /// 释放并发键

    ///

    ///

    ///

    [Microsoft.SqlServer.Server.SqlFunction ]

    public static SqlBoolean DisposeKey(SqlString key)

    {

        try

        {

            _listValue.Remove(key.Value);

            _listGroup.Remove(key.Value);

            return true ;

        }

        catch

        {

            return false ;

        }

    }

};

© 版权声明

相关文章