dbTalk Databases Forums  

срр + odbc + SQLBindParameter = ?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss срр + odbc + SQLBindParameter = ? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
gok
 
Posts: n/a

Default срр + odbc + SQLBindParameter = ? - 05-03-2007 , 01:27 PM






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

sConnectString = "DRIVER=SQL
Server;SERVER=<serverName>;DATABASE=<dbaseName>;Tr usted_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.


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: ??? + odbc + SQLBindParameter = ? - 05-04-2007 , 04:34 PM






gok (gok (AT) aeromap (DOT) com) writes:
Quote:
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 (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.