SQL Server中用户定义聚合的错误结果

时间:2017-12-26 14:21:42

标签: c# sql-server .net-assembly user-defined-aggregate

我一直在尝试在SQL Server中创建自定义聚合函数。我终于让它工作了(用C#编写了程序集)。

目标是连接组中的所有字符串,但结果是空字符串。

这是我在C#中的课程:

[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 32)]
public struct STRING_CONCAT : IBinarySerialize
{
    //result - concatenated string
    SqlString result;

    public void Init()
    {
        //empty the result
        result = "";
    }

    public void Accumulate(SqlString value)
    {
        result += value;
    }

    public void Merge(STRING_CONCAT value)
    {
        Accumulate(value.Terminate());
    }

    public SqlString Terminate()
    {
        return result;
    }

    public void Read(BinaryReader r)
    {
        r.BaseStream.Position = 0;
        result = r.ReadString();
    }

    public void Write(BinaryWriter w)
    {
        w.Write(result.GetUnicodeBytes());
        w.BaseStream.Position = 0;
    }
}

这是T-SQL部分:

CREATE ASSEMBLY [SQLAggregate]
FROM 'C:\CSharp\SQLAggregate\SQLAggregate\bin\Debug\SQLAggregate.dll'
GO

create aggregate STRING_CONCAT
(@string nvarchar(100))
returns nvarchar(4000)
external name [SQLAggregate].[SQLAggregate.STRING_CONCAT] 

1 个答案:

答案 0 :(得分:0)

删除BinaryReader / BinaryWriter上的位置设置,并将 if self.isViewLoaded && (self.view.window != nil) { print("view visible") } 更改为w.Write(result.GetUnicodeBytes());