Unix Technical Forum

help needed converting C float value to DBNUMERIC ...

This is a discussion on help needed converting C float value to DBNUMERIC ... within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:41 AM
Giovanni Azua
 
Posts: n/a
Default help needed converting C float value to DBNUMERIC ...

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



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:41 AM
Erland Sommarskog
 
Posts: n/a
Default Re: help needed converting C float value to DBNUMERIC ...

[posted and mailed, please reply in news]

Giovanni Azua (bravegag@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, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:16 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com