Create simple table with auto increment 'id' field using 'SERIAL' keyword for PostgeSQL
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);
// this ID not exists. Just open SQLQuery (Dont know easiest way)
SQLQuery.SQL.Text := 'SELECT * FROM users WHERE id = -1';
SQLQuery.Active := false;
SQLQuery.Active := true;
SQLQuery.Append();
SQLQuery.FieldByName('Name').AsString := 'John';
// save changes
if (SQLQuery.Transaction.Active) then
begin
SQLQuery.ApplyUpdates();
TSQLTransaction(SQLQuery.Transaction).Commit();
end;
// Now i want to know lasdt ID
SELECT currval(pg_get_serial_sequence('users', 'id'))
ERROR: currval of sequence "users_id_seq" is not yet defined in this session
SELECT last_value FROM users_id_seq;
OK
// i dont want to use 'RETURNING'
INSERT INTO users (name) VALUES ('John') RETURNING id;
is there possibility to get last ID with native FPC functions?
perhaps my SQLQuery.Append() conception is wrong, so please give me an advice with example