By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

help needed converting C float value to DBNUMERIC ...

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
[posted and mailed, please reply in news]

Giovanni Azua (br******@hotmail.com) writes:
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]".


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, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.