Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

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???
0
deighc
Asked:
deighc
  • 5
  • 4
  • 2
1 Solution
 
HilaireCommented:
can you post the command text / stored procedure ?
0
 
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?
0
 
muzzy2003Commented:
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.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
muzzy2003Commented:
I think Hilaire meant the stored procedure itself, not the code that calls it.
0
 
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.
0
 
muzzy2003Commented:
OK, so can you post the stored procedure you are calling then?
0
 
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.

ie.

@<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).

0
 
HilaireCommented:
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)

Hilaire

0
 
muzzy2003Commented:
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.
0
 
deighcAuthor Commented:
Brilliant, that fixed things. Simple when you know how...

Thanks muzzy2003.
0
 
muzzy2003Commented:
No problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now