Xbasic

Using the CallResult LastInsertedIdentity Property

Description

The LastInsertedIdentity property contains the primary key for the last record that was inserted.

Discussion

The LastInsertedIdentity property for a CallResult contains the identity of the record that has just been inserted when executing a SQL INSERT statement or stored procedure that performs an insert on a table. The ID is retrieved as part of (or immediately after) the insert into the database.

Contrast with SQL::Connection LastInsertedIdentity() which returns the last inserted identity for a specific table (where possible.)

Example

dim cn as sql::Connection
dim insert as c =<<%sql%
INSERT INTO employees
(FirstName,LastName,HireDate)
VALUES
(:Firstname,:Lastname,:HireDate);
%sql%

dim args as sql::Arguments
args.set("Firstname","Alma")
args.set("Lastname","Vasquez")
args.set("HireDate",date())

dim id as N = 0
if (cn.open("::Name::AADemo-Northwind")) then
    if (cn.execute(insert,args)) then
        ' insert success
        id = cn.callResult.LastInsertedIdentity
    else
        ' insert failed
    end if
        cn.close()
else
    ' could not open connection 
end if

See Also