Jump to content

HowTo: Using SQL get Updated/Deleted/Inserted rows ID/content


Recommended Posts

Posted (edited)

Some time ago once forum memeber asked about how to get updated rows ID.

In PostgreSQL you can do it like this.

UPDATE "customers" SET "first_name" = 'Jan'  WHERE "last_name" = 'Kowalski'  RETURNING *
UPDATE "customers" SET "first_name" = 'Jan'  WHERE "last_name" = 'Kowalski'  RETURNING "customer_id"

 

EDIT:

You can also use RETURNING clause with INSERT and DELETE:

https://www.postgresql.org/docs/9.0/static/sql-update.html
https://www.postgresql.org/docs/8.2/static/sql-insert.html
https://www.postgresql.org/docs/8.2/static/sql-delete.html
 

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

It's possible to get that done reliably and portably while remaining within the realm of pure SQL. Returning, Output and YouNameIt are clearly outside relational algebra and qualify as proprietary extensions acting as syntactic sugar.

Two obvious ways to perform the same thing, portably:

begin immediate;
create temp table UpdatedCustomers as select * from customers WHERE "last_name" = 'Kowalski';
UPDATE "customers" SET "first_name" = 'Jan' WHERE "last_name" = 'Kowalski';
select * from UpdatedCustomers;
drop table UpdatedCustomers;
commit;

create temp table UpdatedCustomers ("customer_id" integer primary key);
create trigger trUpdateCustomers after update of "customer_id" on "customers"
  begin
    insert or ignore into UpdatedCustomers values (old."customer_id");
  end;
--  .../...
UPDATE "customers" SET "first_name" = 'Jan'  WHERE "last_name" = 'Kowalski'  RETURNING "customer_id";     -- done
select * from UpdatedCustomers;
delete from UpdatedCustomers;

Also I question the semantics of the second proposed statement in the PostgreSQL example:

UPDATE Items SET UnitPrice = UnitPrice * 1.05 WHERE ItemFamilly = 'DVD' RETURNING UnitPrice;

Would that return old or adjusted UnitPrices?

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted
  On 6/14/2016 at 10:01 PM, jchd said:

Would that return old or adjusted UnitPrices?

Expand  

Adjusted, old are a past.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...