property LastInsertId: Int64;
The last identity value is generated in any table in the current session. The function @@IDENTITY is used to identify the last-inserted identity value. To get the value, it's required to run an adiitional request to the server.
To prevent an additional request, it's recommended to generate the identifier value
MSQuery1.SQL.Clear; MSQuery1.SQL.Add('INSERT INTO Dept (dname, loc) VALUES (:dname, :loc)'); MSQuery1.SQL.Add('SET :Identity = SCOPE_IDENTITY()'); MSQuery1.ParamByName('Identity').ParamType := ptOutput; MSQuery1.ParamByName('dname').AsString := 'ACCOUNTING'; MSQuery1.ParamByName('loc').AsString := 'NEW YORK'; MSQuery1.Execute; id := MSQuery1.ParamByName('Identity').AsInteger;