Groups | Blog | Home
all groups > sql server (alternate) > october 2004 >

sql server (alternate) : help needed converting C float value to DBNUMERIC ...


Giovanni Azua
10/28/2004 2:30:39 PM
Hello all,

I need to return back to TSQL two numeric values from an
Extended Stored Procedure developed in C. As result my C
dll program produces two float values, the TSQL side expects
to have exactly: numeric(10, 5) and numeric.

Given that the structure to fill-up is DBNUMERIC defined
as:

typedef struct dbnumeric // Numeric (and decimal)
{
BYTE precision; // Precision
BYTE scale; // Scale
BYTE sign; // 1 = Positive, 0 = Negative
BYTE val[MAXNUMERICLEN]; // Padded little endian value
} DBNUMERIC;

I do not have any clue how to convert the float C datatype
to DBNUMERIC, specifically how to convert the float to an
array of bytes "BYTE val[MAXNUMERICLEN]".

Thanks in advance,
Best Regards,
Giovanni


Erland Sommarskog
10/28/2004 9:32:01 PM
[posted and mailed, please reply in news]

Giovanni Azua (bravegag@hotmail.com) writes:
[quoted text, click to view]

A quick glance makes me believe that srv_convert is able to do the task.
There is some documentation on how to work with DBNUMERIC.

Else you can use the IDataConvert object from OLE DB:

static IDataConvert * data_convert_ptr = NULL;

// Call this once when you DLL is loaded, and keep the object.
ret = CoCreateInstance(CLSID_OLEDB_CONVERSIONLIBRARY,
NULL, CLSCTX_INPROC_SERVER,
IID_IDataConvert,
(void **) &data_convert_ptr);


// Here is a sample routine. Don't bother about the SV, that's a Perl
// thing which holds the double value.
BOOL SV_to_decimal(SV * sv,
BYTE precision,
BYTE scale,
DB_NUMERIC &decimalval)
{
HRESULT ret;

double dbl = SvNV(sv);
ret = data_convert_ptr->DataConvert(
DBTYPE_R8, DBTYPE_NUMERIC, sizeof(double), NULL,
&dbl, &decimalval, NULL, DBSTATUS_S_OK, NULL,
precision, scale, 0);
return SUCCEEDED(ret);
}

Don't really remember which include files that has which, but these
are the ones that I include in my code (which access SQL Server through
SQLOLEDB, and has nothing to do with XPs):

#include <cguid.h>
#include <oledb.h>
#include <oledberr.h>
#include <msdasc.h>
#include <msdadc.h>
#include <msdaguid.h>


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
AddThis Social Bookmark Button