Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to pass decimal(18,2) to a CLR function...


Need to pass decimal(18,2) to a CLR function...

Author
Message
darius.england
darius.england
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 142
Hi. I have a pretty simple piece of C# code:


[SqlFunction(IsDeterministic = true, Name = "ufn_RunningTotalForDecimal")]
public static SqlDecimal RunningTotalsDecimal(SqlDecimal val, SqlByte id, SqlDecimal nullValue)
{
string dataName = string.Format("MulstiSqlRt_{0}_{1}", typeof(SqlByte).FullName, id.IsNull ? 0 : id.Value);

object lastSum = CallContext.GetData(dataName);

SqlDecimal total = lastSum != null ? (SqlDecimal)lastSum : SqlDecimal.Null;

if (!val.IsNull)
total = total.IsNull ? val : total + val;
else
total = total.IsNull ? nullValue : (nullValue.IsNull ? total : total + nullValue);

CallContext.SetData(dataName, total);

return total;
}
}



The problem with this is that when deployed it appears as a function that takes decimal(18,0) for the val and nullValue parameters. I want to have decimal(18,2) instead. How do I change the function above to achieve this?

Thanks.

Best,
Darek
Tonci Korsano
Tonci Korsano
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 46
hi there,

you might want to try this --> [Microsoft.SqlServer.Server.SqlFacet(Precision = 18, Scale = 2)]SqlDecimal val, [Microsoft.SqlServer.Server.SqlFacet(Precision = 18, Scale = 2)]SqlDecimal nullVal

i haven't ever tried this, but sqldecima data type has a precision property as well.

take a look here --> http://www.google.com/#sclient=psy&hl=en&q=sqldecimal+precision&aq=2&aqi=&aql=&oq=&pbx=1&bav=on.1,or.&fp=19d8439716407a1a

good luck,

tonci korsano
darius.england
darius.england
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 142
Hi there. Well, it works for the parameters but it doesn't for the outcome, which is marked as illegal when I try to decorate it with the facet... Any other suggestions, please?
Tonci Korsano
Tonci Korsano
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 46
hi again,

i haven't used sqldecimal ever, but if precision property didn't work, you might want to try method SqlDecimal.ConvertToPrecScale() in your return value.

here is an example of this method --> http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldecimal.converttoprecscale(VS.80).aspx

in general, go here --> http://www.google.com/#hl=en&sugexp=ldymls&xhr=t&q=sqldecimal+return+value+precision&cp=33&pf=p&sclient=psy&aq=f&aqi=&aql=&oq=sqldecimal+return+value+precision&pbx=1&bav=on.1,or.&fp=19d8439716407a1a

it looks like that other programmers had to get around to similar problems you have.

good luck,

tonci korsano
darius.england
darius.england
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 142
Well, I have found the solution. In short:


[SqlFunction(IsDeterministic = true, Name = "ufn_RunningTotalForDecimal")]
[return: Microsoft.SqlServer.Server.SqlFacet(Precision = 18, Scale = 2)]
public static SqlDecimal RunningTotalsDecimal(

[Microsoft.SqlServer.Server.SqlFacet(Precision = 18, Scale = 2)]
SqlDecimal val,

SqlByte id,

[Microsoft.SqlServer.Server.SqlFacet(Precision = 18, Scale = 2)]
SqlDecimal nullValue
)
{
string dataName = string.Format("MulstiSqlRt_{0}_{1}", typeof(SqlByte).FullName, id.IsNull ? 0 : id.Value);

object lastSum = CallContext.GetData(dataName);

SqlDecimal total = lastSum != null ? (SqlDecimal)lastSum : SqlDecimal.Null;

if (!val.IsNull)
total = total.IsNull ? val : total + val;
else
total = total.IsNull ? nullValue : (nullValue.IsNull ? total : total + nullValue);

CallContext.SetData(dataName, total);

return total;
}
}



That required me to purchase membership on www.experts-exchange.com....

Best,
Darek
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search