срр + odbc + SQLBindParameter = ?

срр + odbc + SQLBindParameter = ?

am 03.05.2007 20:27:48 von gok

I'm trying to get back key value for a newly added record "in one
shot":

sConnectString = "DRIVER=SQL
Server;SERVER=;DATABASE=;Trusted_Connection=yes;UID=;PWD=;";
CBindDatabase dbase;
int rs = dbase.OpenEx( sConnectString, CDatabase::useCursorLib |
CDatabase::noOdbcDialog );
this->m_msg.Format("INSERT INTO [dbo].[tTrackLog] (%s) OUTPUT
inserted.RECID VALUES ('%s')",
"[WORKSTATION]", "bla");
dbase.ExecuteSQL(m_msg);



CBindDatabase derived from CDatabase .h:
class CBindDatabase : public CDatabase
{
public:
SQLRETURN RetCode;
SQLINTEGER Value;
SQLINTEGER StrLenOrIndPtr;

public: // attribute modification
virtual void BindParameters(HSTMT hstmt);
};


..cpp:
void CBindDatabase::BindParameters(HSTMT hstmt)
{
RetCode = SQLBindParameter(
hstmt,
1, // Parameter number, ordered sequentially in increasing
parameter order, starting at 1.
SQL_PARAM_OUTPUT,// in or out
SQL_C_SLONG, // value type: SIGNED INTEGER
SQL_INTEGER, // parameter type: integer
0, // column size
0, // decimals digits
&Value, // param value ptr
4, // buff length
&StrLenOrIndPtr); // StrLen_or_IndPtr
}


MS SQL table:

CREATE TABLE [dbo].[tTrackLog]
(
[RECID] [int] IDENTITY(1,1) NOT NULL,
[WORKSTATION] [nchar] (255) NULL
)
ON PRIMARY
GO


Nothing coming out in Value, just garbage.
In MS SQL this Insert query return correct record id.
So I'm wonder:
1. is it possible at all to get back results from ExecuteSQL()
command? Or Bind works only for stored procedures?
2. if it is possible that might be type of parameter in
SQLBindParameter() is wrong? Its returning OK though
3. what could be other options to get back to calling application a
key value for inserted record?
One approach I see is to use AddNew()-Update()-Requery()-MoveLast()
chain but it looks like rather overwhelming.
I need this key to populate child table foreign key in relational
database.

Any comments, suggestions, please.

Re: ??? + odbc + SQLBindParameter = ?

am 04.05.2007 23:34:53 von Erland Sommarskog

gok (gok@aeromap.com) writes:
> CBindDatabase dbase;
> int rs = dbase.OpenEx( sConnectString, CDatabase::useCursorLib |
> CDatabase::noOdbcDialog );
> this->m_msg.Format("INSERT INTO [dbo].[tTrackLog] (%s) OUTPUT
> inserted.RECID VALUES ('%s')",
> "[WORKSTATION]", "bla");
> dbase.ExecuteSQL(m_msg);
>...
> .cpp:
> void CBindDatabase::BindParameters(HSTMT hstmt)
> {
> RetCode = SQLBindParameter(
> hstmt,
> 1, // Parameter number, ordered sequentially in increasing
> parameter order, starting at 1.
> SQL_PARAM_OUTPUT,// in or out
> SQL_C_SLONG, // value type: SIGNED INTEGER
> SQL_INTEGER, // parameter type: integer
> 0, // column size
> 0, // decimals digits
> &Value, // param value ptr
> 4, // buff length
> &StrLenOrIndPtr); // StrLen_or_IndPtr
> }

But there are no parameters in your SQL batch, and least of all any
output parameters. The OUTPUT clause generates a result set.


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx