Home >Database >Mysql Tutorial >How Can I Use Variables and Retrieve the Last Inserted ID in PostgreSQL?
PostgreSQL script variable
Use variables in PostgreSQL
Example:
Get the last inserted ID
<code class="language-sql">DO $$ DECLARE v_List TEXT; BEGIN v_List := 'foobar' ; SELECT * FROM dbo.PubLists WHERE Name = v_List; -- ... END $$;</code>
Please refer to the official PostgreSQL document to understand the complete discussion of using variables in the PostgreSQL script.
<code class="language-sql">DO $$ DECLARE lastid bigint; BEGIN INSERT INTO test (name) VALUES ('Test Name') RETURNING id INTO lastid; SELECT * FROM test WHERE id = lastid; END $$;</code>
The above is the detailed content of How Can I Use Variables and Retrieve the Last Inserted ID in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!