Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

postgresql - How do I show the num records affected in a rolled-back transaction in Postgres pgAdmin?

In SQL Server when I would use Mgmt Studio to make changes to data I would often wrap the queries in a transaction to semi-verify the data being changed was as expected.

Example:

BEGIN TRANSACTION;
UPDATE mytable SET col1 = 'blah' WHERE col2 = x and col3 = y;
ROLLBACK;

The Messages tab would display

(1 row affected)

I would see that "1 row affected", which agreed with what I expected, then I would just change ROLLBACK to COMMIT and run it again to make the changes stick.

Is there a typical approach to do this in pgAdmin? When I run the query in the transaction, it only displays in the Messages tab the time it took to execute:

Query returned successfully in 141 msec.

As a clunky work-around I can use a plpgsql script with the DIAGNOSTICS like this:

BEGIN TRANSACTION;
DO $$
DECLARE rowCount int;
BEGIN
   UPDATE mytable SET col1 = 'blah' WHERE col2 = x and col3 = y;
   GET DIAGNOSTICS rowCount = row_count;
   RAISE NOTICE 'num rows affected = %', rowCount;
END; $$;
ROLLBACK;

The Messages tab then shows the following output:

NOTICE:  num rows affected = 1
ROLLBACK

Query returned successfully in 125 msec.

Is there any better approach than that ugly script?

question from:https://stackoverflow.com/questions/66052442/how-do-i-show-the-num-records-affected-in-a-rolled-back-transaction-in-postgres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...