Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Databases
»
PostGres - Getting last inserted ID
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Parameter passing odditie...
by
Thaddy
[
Today
at 05:19:18 pm]
Lazarus for Windows on aa...
by
msintle
[
Today
at 05:17:40 pm]
Xcode Command Line Tools ...
by
Joseph
[
Today
at 05:16:39 pm]
Compile/Convert Delphi pr...
by
Martin_fr
[
Today
at 05:09:14 pm]
Offical launch of the 1 B...
by
Hartmut
[
Today
at 04:49:29 pm]
what to do if my target M...
by
nickysn
[
Today
at 04:16:02 pm]
trunk compiler bug for mi...
by
nickysn
[
Today
at 04:03:44 pm]
compiler error in unit
by
paule32
[
Today
at 03:49:50 pm]
how to tweek fpmake to co...
by
Key-Real
[
Today
at 02:33:27 pm]
Can function be used for ...
by
VisualLab
[
Today
at 02:15:30 pm]
Multithreading - synchron...
by
mika
[
Today
at 01:48:51 pm]
A fairly simple sound sol...
by
KodeZwerg
[
Today
at 01:48:26 pm]
May be useful to somebody
by
KodeZwerg
[
Today
at 12:38:22 pm]
match automatic highlight...
by
Чебурашка
[
Today
at 11:35:14 am]
Tchart with dual numbers ...
by
wp
[
Today
at 10:48:18 am]
Generics - correct syntax
by
VisualLab
[
Today
at 10:23:16 am]
add new targets to make
by
Key-Real
[
Today
at 09:38:23 am]
Debugger crashes with App...
by
Martin_fr
[
Today
at 09:29:46 am]
Copyrights Q
by
BrassGear
[
Today
at 09:13:14 am]
[SOLVED] how to build the...
by
Key-Real
[
Today
at 09:12:33 am]
Quirky windows
by
tetrastes
[
Today
at 08:59:06 am]
How can I draw a rectangl...
by
Handoko
[
Today
at 07:30:55 am]
Draw a line in the Scroll...
by
Handoko
[
Today
at 07:27:54 am]
web development using pas...
by
Nate897
[
Today
at 02:04:05 am]
questionable function def...
by
440bx
[
Today
at 01:35:28 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: PostGres - Getting last inserted ID (Read 15155 times)
kapibara
Hero Member
Posts: 610
PostGres - Getting last inserted ID
«
on:
March 10, 2017, 10:16:42 pm »
Postgres has a feature that gives back the ID of an inserted record directly, without having to do a select later: INSERT RETURNING. Added an example to the WIKI.
http://wiki.lazarus.freepascal.org/postgres#How_To
«
Last Edit: February 17, 2018, 04:37:38 am by kapibara
»
Logged
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit
goodname
Sr. Member
Posts: 297
Re: PostGres - Getting last inserted ID
«
Reply #1 on:
March 11, 2017, 02:28:58 pm »
Thankyou kapibara. The RETURNING clause was added to SQLdb a long time ago and it is finally documented in the wiki. The documentation is not really complete. RETURNING in PostgreSQL is more flexible than most database engines. It works with INSERT, UPDATE and DELETE statements and can be any list of fields that would be found in a SELECT field list.
Code: SQL
[Select]
[+]
[-]
INSERT
INTO
films
(
filmname
)
VALUES
(
'val'
)
RETURNING id;
-- Returns id's for newly created rows.
INSERT
INTO
films
(
filmname
)
VALUES
(
'val'
)
RETURNING id
,
kind;
-- Returns id and kind fields in newly created rows.
INSERT
INTO
films
(
filmname
)
VALUES
(
'val'
)
RETURNING
*
;
-- Returns all fields in newly created rows.
UPDATE
films
SET
kind
=
'Dramatic'
WHERE
kind
=
'Drama'
RETURNING id;
-- Returns id's of updated rows.
UPDATE
films
SET
kind
=
'Dramatic'
WHERE
kind
=
'Drama'
RETURNING id
,
filmname;
-- Returns id and film names of updated rows.
UPDATE
films
SET
kind
=
'Dramatic'
WHERE
kind
=
'Drama'
RETURNING
*
;
-- Returns all fields of updated rows.
DELETE
FROM
films RETURNING id;
--Returns id's of deleted rows.
DELETE
FROM
films RETURNING id
,
filmname;
--Returns id's and film names of deleted rows.
DELETE
FROM
films RETURNING
*
;
-- Returns all fields of deleted rows.
Edit: Fixed syntax errors in insert statements.
«
Last Edit: March 13, 2017, 04:40:30 pm by goodname
»
Logged
kapibara
Hero Member
Posts: 610
Re: PostGres - Getting last inserted ID
«
Reply #2 on:
March 12, 2017, 02:45:14 am »
Great example, goodname, updated wiki.
«
Last Edit: March 12, 2017, 04:44:14 am by kapibara
»
Logged
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit
JD
Hero Member
Posts: 1848
Re: PostGres - Getting last inserted ID
«
Reply #3 on:
March 13, 2017, 10:28:34 am »
This is one of the many reasons why PostgreSQL has become my favourite database. It has sooo many nice features that help save time and effort.
JD
Logged
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)
mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView
Abelisto
Jr. Member
Posts: 91
Re: PostGres - Getting last inserted ID
«
Reply #4 on:
March 13, 2017, 02:06:41 pm »
Quote from: goodname on March 11, 2017, 02:28:58 pm
can be any list of fields that would be found in a SELECT field list.
BTW not only fields but also constants and expressions:
Code: SQL
[Select]
[+]
[-]
INSERT
INTO
films
(
filmname
)
VALUE
'val'
RETURNING id
,
'a'
AS
a
,
id
*
2
AS
doubled_id
,
CASE
WHEN
id
>
100
THEN
'a'
ELSE
'b'
END
AS
foo;
Logged
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Databases
»
PostGres - Getting last inserted ID
TinyPortal
© 2005-2018