|
Posted by gok on 05/03/07 18:27
I'm trying to get back key value for a newly added record "in one
shot":
sConnectString = "DRIVER=SQL
Server;SERVER=<serverName>;DATABASE=<dbaseName>;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.
Navigation:
[Reply to this message]
|