Board index » delphi » problem with Sybase autoincrement field

problem with Sybase autoincrement field

hi all,

I have a table that has a autoincrement field.When I insert a record in that
table , I want to get that fields' value in my delphi application.I use
TQuery for it.TQuery has a procedure called AfterPost.
AfterPost doesn't work as i want.I think this problem result from Sybase.

Anyone can help me?

thanks.

 

Re:problem with Sybase autoincrement field


Since the increment is happening on the server (not by Delphi) you have to
close/open the query to refresh the results.
Here are some links with additional info...
http://community.borland.com/article/0,1410,20847,00.html
http://community.borland.com/article/0,1410,16123,00.html

Good luck,
krf

?etin ?aelar SEYHAN <csey...@mmo.org.tr> wrote in message
news:3bde9bbe_2@dnews...

Quote
> hi all,

> I have a table that has a autoincrement field.When I insert a record in
that
> table , I want to get that fields' value in my delphi application.I use
> TQuery for it.TQuery has a procedure called AfterPost.
> AfterPost doesn't work as i want.I think this problem result from Sybase.

> Anyone can help me?

> thanks.

Re:problem with Sybase autoincrement field


Quote
Kevin Frevert wrote:
> Since the increment is happening on the server (not by
> Delphi) ...

(Allow me to drop in with my experience on this issue,
assuming you are using Sybase Anywere)

This is true, but we struggled with this problem too and
found that a very good 3rd party BDE replacement
components "nativeDb" (www.nativedb.com) was actually able
to return the auto-inc value immediately. This was one of
the main reasons we switched from the BDE.

These comps makes the auto-inc value available right after
the Post call on the dataset (or in an AfterPost event),
and can be retrieved just by a regular
".FieldByName('auto-inc-field').AsInteger" expression.

(For example, assume a table with 2 fields "id" and "name"
where "id" is defined as auto-incremental):

MyDataset1.Insert;
MyDataset1.FieldByName('name').AsString := 'Chris';
MyDataset1.Post;
Caption := MyDataset1.FieldByName('id').AsString;

In this last line, Caption is set to the auto-inc value
as assigned to it by the DB server. This is a very nice
feature of these components, and I recommend you to check
these comps out yourself, if leaving the BDE is an option
for you.

For us, this was extremely important because we heavily
rely on auto-incremental fields "all over the place".

-Chris

Re:problem with Sybase autoincrement field


The last auto increment value can be retrieved in SQL Anywhere as such:

function GetLastAutoInc: Integer
begin
  // Retreive last AutoInc value
  Query1.Close;
  Query1.SQL.Text := 'SELECT @@identity FROM Dummy';
  Query1.Open;
  Result := Query1.Fields[0].AsInteger;
  Query1.Close;

end; // function GetLastAutoInc: Integer

HTH - Mike

"?etin ?aelar SEYHAN" <csey...@mmo.org.tr> wrote in message
news:3bde9bbe_2@dnews...

Quote
> hi all,

> I have a table that has a autoincrement field.When I insert a record in
that
> table , I want to get that fields' value in my delphi application.I use
> TQuery for it.TQuery has a procedure called AfterPost.
> AfterPost doesn't work as i want.I think this problem result from Sybase.

> Anyone can help me?

> thanks.

Re:problem with Sybase autoincrement field


Quote
> The last auto increment value can be retrieved in SQL Anywhere as such:
>   Query1.SQL.Text := 'SELECT @@identity FROM Dummy';

This is all well, but suppose the main-table you insert
into (with an auto-inc field) has defined a TRIGGER which
in turn inserts into another table (also with an auto-inc
field), what will then be the result of "SELECT @@identity" ?
To my knowledge, "@@identity" has a global database scope.

-Chris

Re:problem with Sybase autoincrement field


Chris,

You are correct.  @@identity contains the *last* autoinc value assigned on
that connection.  I should have pointed that out in my message.  Thank you.

- Mike

Quote
"Christoffer Sulvan" <chris_NOS...@bnccs.com> wrote in message

news:3be01309_2@dnews...
Quote
> > The last auto increment value can be retrieved in SQL Anywhere as such:
> >   Query1.SQL.Text := 'SELECT @@identity FROM Dummy';

> This is all well, but suppose the main-table you insert
> into (with an auto-inc field) has defined a TRIGGER which
> in turn inserts into another table (also with an auto-inc
> field), what will then be the result of "SELECT @@identity" ?
> To my knowledge, "@@identity" has a global database scope.

> -Chris

Other Threads