retrieve primary key after insert

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Tony49
Posts: 21
Joined: Mon 26 Jan 2009 17:10

retrieve primary key after insert

Post by Tony49 » Mon 31 Jan 2011 15:09

Can you tell me if it's possible to retrieve primary key (generated by a trigger before insert) after my insert?

I set ParamType to Db::ptOutput. The retrieve value is empty : pOraSQL->ParamByName("ID")->AsString.

Can you help me?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Tue 01 Feb 2011 08:28

Hello,

To retrieve primary key (which is created in a trigger) you can use the following code:

TOraSession *OraSession = new TOraSession(NULL);
OraSession->Server = "SERVER";
OraSession->Username = "USER";
OraSession->Password = "PASSWD";
OraSession->Connect();

TOraSQL *OraSQL = new TOraSQL(NULL);
OraSQL->Session = OraSession;
OraSQL->SQL->Text = "INSERT INTO TEST(TEXT) VALUES(:TEXT) RETURNING ID INTO :ID";
OraSQL->ParamByName("TEXT")->ParamType = ptInput;
OraSQL->ParamByName("TEXT")->DataType = ftString;
OraSQL->ParamByName("TEXT")->AsString = "TEST";
OraSQL->ParamByName("ID")->ParamType = ptOutput;
OraSQL->ParamByName("ID")->DataType = ftInteger;
OraSQL->Execute();
ShowMessage(OraSQL->ParamByName("ID")->AsString);

Tony49
Posts: 21
Joined: Mon 26 Jan 2009 17:10

Post by Tony49 » Tue 01 Feb 2011 08:56

Thank's a lot. ;-)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Tue 01 Feb 2011 09:23

Hello,

If any other questions come up, please contact us.

Post Reply