Proposal for MSSQL issue getting identity column

The forum for ZeosLib 7.2 Report problems. Ask for help, post proposals for the new version and Zeoslib 7.2 features here. This is a forum that will be edited once the 7.2.x version goes into RC/stable!!

My personal intention for 7.2 is to speed up the internals as optimal a possible for all IDE's. Hope you can help?! Have fun with testing 7.2
Post Reply
ChrisCross
Fresh Boarder
Fresh Boarder
Posts: 8
Joined: 21.02.2011, 17:03

Proposal for MSSQL issue getting identity column

Post by ChrisCross »

On MSSQL we can use 3 ways of getting last inserted identity in a table

1.@@IDENTITY which gets last inserted identity
2. SCOPE_IDENTITY() which gets last inserted identity in current scope
3. IDENT_CURRENT( 'table_name' ) which gets the last identity value generated for a specified table or view.

If we have a table let's say PEOPLE with IDPeople int identity(1,1) all 3 functions return the same value if we are doing a simple insert.
If we have also a trigger that logs the operations made to the PEOPLE table into a separate log table which has also an identity column then 1st function will return the id inserted in the log table and
the other 2 function will return the last IDPeople inserted in the PEOPLE table.

My proposal is to use then SCOPE_IDENTITY function instead of @@IDENTITY function for getting the wright identity in the table used.
The 3rd function is the best option but we have to get current table name in order to use it.

I replaced the @@Identity with SCOPE_IDENTITY() for example in ZDbcDbLibResultSet.pas

Code: Select all

procedure TZDBLibCachedResolver.PostUpdates(Sender: IZCachedResultSet;
  UpdateType: TZRowUpdateType; OldRowAccessor, NewRowAccessor: TZRowAccessor);
var
  Statement: IZStatement;
  ResultSet: IZResultSet;
  I: Integer;
begin
  inherited PostUpdates(Sender, UpdateType, OldRowAccessor, NewRowAccessor);

  { Defines an index of autoincrement field. }
  if FAutoColumnIndex = -1 then
    for I := FirstDbcIndex to Metadata.GetColumnCount{$IFDEF GENERIC_INDEX}-1{$ENDIF} do
      if Metadata.IsAutoIncrement(I) then
      begin
        FAutoColumnIndex := I;
        Break;
      end;

  if (UpdateType = utInserted) and (FAutoColumnIndex > InvalidDbcIndex)
    and OldRowAccessor.IsNull(FAutoColumnIndex) then
  begin
    Statement := Connection.CreateStatement;
    ResultSet := Statement.ExecuteQuery('SELECT SCOPE_IDENTITY()');
    try
      if ResultSet.Next then
        NewRowAccessor.SetLong(FAutoColumnIndex, ResultSet.GetLong(FirstDbcIndex));
    finally
      ResultSet.Close;
      Statement.Close;
    end;
  end;
end;
What is your opinion regarding this ?
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: Proposal for MSSQL issue getting identity column

Post by EgonHugeist »

Hi!

I like ready patches! I did google your suggestions ... SCOPE_IDENTITY seems to be supported since SQL Server 2008.

Not a problem if you have an idea how we can determine a 2008+ Server. Any ideas?
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
ChrisCross
Fresh Boarder
Fresh Boarder
Posts: 8
Joined: 21.02.2011, 17:03

Re: Proposal for MSSQL issue getting identity column

Post by ChrisCross »

Hi!
Acording to this http://technet.microsoft.com/en-us/libr ... l.80).aspx it's also suported since sql2000.

I'll try it tomorow on an instance but i'm sure of it because i made this modification some time ago and we have clients on sql2000 (hoping to upgrade soon).
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: Proposal for MSSQL issue getting identity column

Post by EgonHugeist »

Geat news!

But there is still the Version check missing. Note we support MSSQL from 6.5? to 2012 and 2014(untested)? So apply this patch the way it is would lead to new bug-tickets..
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
marcov
Senior Boarder
Senior Boarder
Posts: 95
Joined: 24.06.2010, 09:17

Re: Proposal for MSSQL issue getting identity column

Post by marcov »

6.5,7 and 2000 are already out of extended support by MS it seems:

http://support2.microsoft.com/lifecycle ... &alpha=sql

For sql 2000 that date is fairly recent (2013), but the rest is *old*
lrrosa
Fresh Boarder
Fresh Boarder
Posts: 4
Joined: 26.09.2013, 19:09

Re: Proposal for MSSQL issue getting identity column

Post by lrrosa »

There is a global variable that returns all the information about the current version:

Code: Select all

SELECT @@VERSION
And there is a function easier to parse that returns just the version number:

Code: Select all

SELECT SERVERPROPERTY('productversion')
But I don't know if this second method is supported on SQL Server < 2000.
Post Reply