🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.databases
Article #33

Getting @@Identity from SQL Server via ODBC Driver

#33
From: Maurizio Baldi
Date: Wed, 24 Nov 1999 00:00
93 lines
3888 bytes
I need to access a SQL Server 6.5 via ODBC;
I have this problem on master - details Form;
I'd like to get ID of the inserted master record;

The following code is correct if I use native Delphi Driver, but if I use
ODBC Driver the variable id returns 0!

Can you help me?

Thanks in advance.

Maurizio Baldi, Abel Key Scrl, Florence, Italy-

DBUPD : TUpdateSQL;
DBQ : TQuery;
DBQ_Exec : TQuery;
id : integer;
....
DBQ.UpdateObject := DBUPD;
DBQ.CachedUpdates := True;
DBQ.RequestLive := True;
DBUPD.Sql[ukInsert].Text := 'Insert into Books (title, author) values
(''1984'',''Orwell'')';

DBQ.Append;
[some field operations....]
DBQ.Post;

DBQ.Database.StartTransaction;
try
    DBUPD.Apply(ukInsert);
    with DBQ_Exec do begin
        if Active then Close;
        SQL.Text := 'Select @@Identity As ID';
        Open;
        id := FieldByName('ID').AsInteger;
    end;
    [operations with details...]
    DBQ.Database.Commit;
except
    DBQ.Database.Rollback;
end;

These are the SQL trace in the 2 cases...

NATIVE DRIVER:
-- 24/11/99 11:40:09,073 SQL (ID=141, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host=''(ffc0a5b5) )
BEGIN TRAN
go
-- 24/11/99 11:40:18,206 SQL (ID=141, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host=''(ffc0a5b5) )
Insert into Books (title, author) values ('1984','Orwell')
go
-- 24/11/99 11:40:20,390 SQL (ID=141, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host=''(ffc0a5b5) )
Select @@IDENTITY AS ID
go

ODBC DRIVER:
-- 24/11/99 11:29:07,653 SQL (ID=135, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
go
-- 24/11/99 11:29:49,173 SQL (ID=135, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
set implicit_transactions on
go
-- 24/11/99 11:29:49,173 SQL (ID=135, SPID=17, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
go
-- 24/11/99 11:30:17,383 New connection (ID=136, SPID=20,
User=sa(AK_PD\BALDMA), App='', Host='BALDMA'(ffc0aafd) )
Insert into Books (title, author) values ('1984','Orwell')
-- 24/11/99 11:30:17,403 SQL (ID=136, SPID=20, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
set quoted_identifier on use "GBG"
go
-- 24/11/99 11:30:17,603 SQL (ID=136, SPID=20, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
exec sp_server_info 500 select 501,NULL,1 where 'a'='A' select
504,c.name,c.description,c.definition from master.dbo.syscharsets
c,master.dbo.syscharsets c1,master.dbo.sysconfigures f where f.config=1123
and f.value=c1.id and c1.csid=c.id set textsize 2147483647 set ansi_defaults
on set cursor_close_on_commit off set implicit_transactions off
go
-- 24/11/99 11:30:17,623 SQL (ID=136, SPID=20, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
set implicit_transactions on
go
-- 24/11/99 11:30:17,633 SQL (ID=136, SPID=20, User=sa(AK_PD\BALDMA),
App='', Host='BALDMA'(ffc0aafd) )
Select @@IDENTITY AS ID
Technical Details
Message-ID: <383bd449.0@news.dada.it>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: ffff5,f7dcc4fd211515d4,start
X-Google-Attributes: gidffff5,public
X-Google-Thread: ff75c,f7dcc4fd211515d4,start
X-Google-Attributes: gidff75c,public
X-Google-Thread: 10de66,f7dcc4fd211515d4,start
X-Google-Attributes: gid10de66,public
X-Google-Thread: f6386,f7dcc4fd211515d4,start
X-Google-Attributes: gidf6386,public
From: "Maurizio Baldi" <m.baldiNOSPAM@abeline.it>
Subject: Getting @@Identity from SQL Server via ODBC Driver
Date: 1999/11/24
Message-ID: <383bd449.0@news.dada.it>
X-Deja-AN: 552474621
X-Trace: 24 Nov 1999 13:04:25 +0100, srvi.abeline.it
Organization: Abel Key Scrl
X-MSMail-Priority: Normal
X-Priority: 3
Newsgroups: comp.lang.pascal.delphi.databases,borland.public.delphi.database.desktop,borland.public.delphi.database.multi-tier,borland.public.delphi.database.sqlservers
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300