<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 14, 2014 at 9:53 AM, Reimar Grabowski <span dir="ltr"><<a href="mailto:reimgrab@web.de" target="_blank">reimgrab@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">On Thu, 13 Nov 2014 11:33:58 -0300<br>
silvioprog <<a href="mailto:silvioprog@gmail.com">silvioprog@gmail.com</a>> wrote:<br>
<br>
> I use an own "TPreparedStatement" (equivalent to TSQLQuery prepared +<br>
> Params, but my structure does not uses TDataset) that I get the last<br>
> inserted ID with (pseudo codes):<br>
</span><snip><br>
Sorry, you lost me there, but not a problem (see my response to Marcos).<br>
<br>
Thanks anyway</blockquote></div><div><br></div><div>I saw.</div><div><br></div><div>The "problem" which I see is just:</div><div><br></div><div>Query.SQL.Text := '<span style="font-family:arial,sans-serif;font-size:13px">SELECT LAST_INSERT_ID() AS SomeNameIChoose;';</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Query.Open; << here</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">ID := Query.Fields[0].AsInt64;</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">This approach is ugly IMHO.</font></div><div><br></div><div>So I use "INSERT X (FIELDS) VALUES (PARAMS) RETURNING PK", returning the generated ID in same SQL cursor which I used to prepare my statement. This idea is from JDBC and I'm using it daily. Works fine in all popular SGBDs. =)</div><div>Even when the SGBD does not provides the "RETURNING" feature, I use the SQL cursor from the database driver to generate it without TDataset.Open.</div><div><br></div><div>My complete code works only in FPC 2.7.1+, but in short words I get the generated ID via buffer (pointer) on database driver.</div><div><br></div>-- <br><div class="gmail_signature">Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a></div>
</div></div>