Update a table and display updated rows with a single SQL command

无人久伴 提交于 2019-12-11 09:03:23

问题


I need your help with PostgreSQL. I have a homework in which I have to update a column of a table and display all the information that has been updated, but I have to use a single command to do all that. Even worse, I just can use basic clauses like SELECT, UPDATE, SET, WHERE etc.

Is this possible? I didn't find any example.

I've tried several combinations like:

SELECT * FROM customer 
(UPDATE custumer SET bithdate = bithdate + INTERVAL '1 DAY'
 WHERE   bithcity = 'New York');

This didn't work!


回答1:


Since this is homework, I'll leave something for you.

Single SQL statement

Start reading about the RETURNING clause of the UPDATE command in the manual.

Another alternative (for more complex scenarios) would be a data-modifying CTE. You still need the RETURNING clause for that.

Or you could create a function you can call in a single statement, but that would violate your requirement of only using simple DML commands.

Single transaction

If more than a single statement is allowed, another option would be a simple UPDATE plus simple SELECT wrapped into a transaction.



来源:https://stackoverflow.com/questions/15992365/update-a-table-and-display-updated-rows-with-a-single-sql-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!