Informix: UPDATE with SELECT - syntax?

一个人想着一个人 提交于 2020-02-02 16:26:20

问题


I wanna update my table for all persons whoes activity lasted toooo long. The update should correct one time and for the subsequent rows I need to deal with new result. So thought about something like

UPDATE summary_table st
SET st.screen_on=newScreenOnValue
    st.active_screen_on=st.active_screen_on-(st.screen_on-newScreenOnValue) --old-value minus thedifference
FROM (
      SUB-SELECT with rowid, newScreenOnValue ... JOIN ... WHERE....
     ) nv
WHERE (st.rowid=nv.rowid)

I know that I can update the first and the second value directly, by rerunning the same query. But my problem is the costs of the subselect seems quite high and therefore wanna avoid a double-update resp. double-run of the same query.

The above SELECT is just a informal way of writting what I think I would like to get. I know that the st doesn't work, but I left it here for better understanding. When I try the above statement I always get back a SyntaxError at the position the FROM ends.


回答1:


This can be achieved as follows:

UPDATE summary_table st
SET (st.screen_on, st.active_screen_on) = 
    ((SELECT newScreenOnValue, st.active_screen_on-(st.screen_on-newScreenOnValue)
    FROM ... 
    JOIN... 
    WHERE..))
[WHERE if any additional condition required];

The above query works perfectly fine on informix tried and tested until you make any errors in the FROM, JOIN, WHERE clauses.

Cheers !




回答2:


  1. Syntax error because a comma is missing between the first and second columns you're updating.

  2. Never use ROWID's, they're volatile and also not used by default with IDS, unless you specify so.

  3. Why are you using a subquery?



来源:https://stackoverflow.com/questions/17701344/informix-update-with-select-syntax

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