Command object and decimal data type

I'm trying to update a table that contains a decimal data type by using a Command object.

But all numeric values to the right of the decimal point are lost and I'm only left with the integer portion of the number after executing the command object.

I'm setting both the Precision and NumericScale properties of the Parameter object that corresponds with decimal field in the db but I still have the same problem.

Code looks something like this for the parameter related to the decimal field in the db table (where 'cmd' is a command object):


set cmdParam = cmd.CreateParameter()
cmdParam.Name = "@<name of param>"
cmdParam.Type = adDecimal
cmdParam.Direction = adParamInput
cmdParam.Size = 5
cmdParam.Precision = 5
cmdParam.NumericScale 2
cmdParam.Value = 2.13


This updates the table so that the decimal field has a value of 2 (which is obviously NOT what I want).
The Parameter object values for Size, Precision and NumericScale are set to the exact same values as the corresponding attributes of the field in the database (ie. Length, Precision and Scale respectively).

Any clues why I'm losing the data to the right of the decimal point???
LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

can you post the command text / stored procedure ?
deighcAuthor Commented:
That's probably more trouble than it's worth because I'm actually parsing an XML file and setting the parameters of the command object programmatically. There are quite a few loops and functions involved and you'd probably lose interest if I posted all the code here ;-)

Is there some additional information I could give you instead?
Have you got On Error Resume Next or something? It's just that you have:

cmdParam.NumericScale 2

rather than:

cmdParam.NumericScale = 2

If this line is failing, the numeric scale may keep a value of 0, and with On Error Resume Next you wouldn't notice.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I think Hilaire meant the stored procedure itself, not the code that calls it.
deighcAuthor Commented:
No, that's just a typo in my pseudo-code. The "real" code has no error handling at that point and no errors are occuring. The database updates fine - it's just that I lose the fractional part of the number.
OK, so can you post the stored procedure you are calling then?
deighcAuthor Commented:
> I think Hilaire meant the stored procedure itself

Doh!! Good point.

The SP is REALLY big because there are lots of the fields in the update statement and lots of input parameters (so again, I don't want to scare anyone away with crap loads of dull code). But otherwise it's a very straightfoward SP that takes a whole lot of input parameters and simply updates a table.

The SP parameter corresponding to the decimal field in the db table is declared as a 'decimal' type.


@<param name> decimal

I've also tried 'numeric' but this doesn't work either (and it's my understanding that they're just different names for the same thing anyway).

The problem might still be in the stored procedure itself
Say you have a Qty field and you want to calculate a percentage Qty in group vs Total Qty.

You'll need to explicitly cast Quantities to a proper datatype or you'll always get truncated values, as all columns involved in the calculation are INT datatype

Seeing the code would really help (you can strip parts of ti if yo want)


You must specify the precision and scale for your decimal parameter, otherwise the scale of it will default to 0. Declare it as decimal(5, 2) instead of just decimal.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deighcAuthor Commented:
Brilliant, that fixed things. Simple when you know how...

Thanks muzzy2003.
No problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.