0
SQL Server 2008에 대한 일부 사용자 지정 .Net 확장을 작성하고 있습니다. 그 중 하나는 10 진수 집합을 십진수로 집계해야하는 사용자 정의 집계입니다 .SQL CLR 확장 (사용자 정의 집계)의 반올림 오류
내 문제의 범위를 좁히기 위해 간단한 Const
집계를 사용하고 있는데, 단순히 상수 10 진수 값을 반환합니다. SQL Server에 대한 사용자 정의 집계로이를 추가 할 때 반환되는 값은 항상 반올림 : 나는 무엇을 놓치고
SELECT dbo.Const(n, 2.5) from (select 1 n) x -- returns 3, not 2.5
?
여기에 코드입니다 :
using System;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
using SqlServer.Clr.Extensions.Aggregates.Implementation;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined,MaxByteSize = -1)]
public class Const : IBinarySerialize
{
private decimal _constValue;
public void Init() {}
public void Accumulate(SqlDecimal value, SqlDecimal constValue)
{
_constValue = constValue.Value;
}
public void Merge(Const value) {}
public SqlDecimal Terminate()
{
return new SqlDecimal(_constValue);
}
public void Read(BinaryReader r)
{
_constValue = r.ReadDecimal();
}
public void Write(BinaryWriter w)
{
w.Write(_constValue);
}
}
가 SqlServer.Clr.Extensions.dll
라는 이름의 어셈블리에이 코드를 컴파일합니다. 다음 스크립트는 예기치 않은 동작이 SQL 서버에 추가하고 확인하는 데 사용할 수 있습니다 :
use [MyDb] -- replace with your db name
go
-- drop the Const aggregate if it exists
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Const]'))
DROP AGGREGATE [Const]
GO
-- drop the assembly if it exists
IF EXISTS (SELECT * FROM sys.assemblies asms WHERE asms.name = N'SqlServer.Clr.Extensions' and is_user_defined = 1)
DROP ASSEMBLY [SqlServer.Clr.Extensions]
GO
-- import the assembly
CREATE ASSEMBLY [SqlServer.Clr.Extensions]
AUTHORIZATION [dbo]
FROM 'C:\Path\To\SqlServer.Clr.Extensions.dll'
WITH PERMISSION_SET = SAFE
GO
CREATE AGGREGATE [Const](@input decimal, @constValue decimal)
RETURNS decimal
EXTERNAL NAME [SqlServer.Clr.Extensions].[Const] -- put the Const class is in the global namespace
GO
SELECT dbo.Const(n, 2) from (select 1 n) x
SELECT dbo.Const(n, 2.5) from (select 1 n) x
에
@constvalue decimal(13,2)
에@constvalue decimal
을 변경해야 할 나는 당신이 당신의 소수점의 정밀도를 설정해야합니다 생각합니다. CREATE AGGREGATE [Const] (@ 입력 십진수, @constValue 십진수)에서 @constValue decimal을 @constValue decimal (13,2)로 변경해보십시오. –@Verrigo 오 물론 저 ... 어쩌면 답으로 의견을 써야합니다. 그래서 받아 들일 수 있을까요? – jeroenh