我正在从另一个表更新Oracle表,然后我需要使用sysdate为所有已更新的行更新date_modified字段。我的代码目前看起来像这样:
UPDATE
table1 t1
SET
field1 = (SELECT t2.value
FROM table2 t2
WHERE t1.r_id = t2.r_id
AND t2.other in (2,3,4))
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.r_id = t2.r_id
AND t2.value IS NOT NULL);
如何添加命令以将date_modified更新为sysdate?我尝试了第二个更新命令,但这是更新整个表。当我尝试添加以下子句时:
set
date_modified = sysdate (WHERE t2.value IS NOT NULL
AND t2.other in (2,3,4))
这似乎无法引用t2,并且在完整的表名中写入也不起作用。
这在SQL中可行吗?我知道我不能在更新语句中使用JOIN,我不确定是否有其他规则阻止我这样做。
感谢
答案 0 :(得分:2)
update table1 t1
set
field1 = (...)
, date_modified = sysdate -- << ADD
where exists...
答案 1 :(得分:0)
也许这就是你想要的:
UPDATE table1 t1
SET field1 = (SELECT t2.value
FROM table2 t2
WHERE t1.r_id = t2.r_id AND t2.other in (2,3,4)
),
data_modified = sysdate
WHERE NOT EXISTS (SELECT t2.value
FROM table2 t2
WHERE t1.r_id = t2.r_id AND t2.value IS NOT NULL AND
t1.field1 = t2.value
);
请注意WHERE
子句的细微更改。这可以确保新值与旧值不同。
答案 2 :(得分:0)
嗨,非常好的问题。在这里你可以使用Case语句,
UPDATE table1 t1 set field1 = (SELECT t2.value
FROM table2 t2
WHERE t1.r_id = t2.r_id
AND t2.other in (2,3,4))
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.r_id = t2.r_id
AND t2.value IS NOT NULL),
date_modified = case when t2.value IS NOT NULL
AND t2.other in (2,3,4) then sysdate
希望你去。让我知道会帮助你。