我在Oracle 10g上有这个查询:
UPDATE "SCHEMA1"."CELLS_GLIST"
SET ("GLIST_VALUE_ID", "USER_ID", "SESSION_ID") = (
SELECT "GLIST_VALUE_ID", 1 AS "USER_ID", 123456 AS "SESSION_ID"
FROM "SCHEMA1"."GLISTS_VALUES_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_ID"=67)
AND ("GLIST_VALUE_DESC" = (
SELECT "GLIST_VALUE_DESC"
FROM "BMAN_TP1"."GLISTS_VALUES_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_VALUE_ID"="CELLS_GLIST"."GLIST_VALUE_ID")
))
)
WHERE EXISTS (......)
它一直说 ORA-01407:无法更新(“SCHEMA1”。“CELLS_GLIST”。“SESSION_ID”)为NULL
“SESSION_ID”显然是 Not Nullable ,但我实际上是将值传递给该字段,所以我不明白这个问题。
答案 0 :(得分:0)
根据您的评论,我读到您似乎想要在目标表中写入默认记录,以防子查询未返回任何记录。因此,用短语查询的正确方法是使用MERGE
语句:
MERGE INTO "SCHEMA1"."CELLS_GLIST" dst
USING (
-- rephrase your subquery here. This is your "merge data source". The number
-- of records returned in this subquery will correspond to the number of
-- affected records in dst
) src
ON (
-- the missing exists condition here. Everytime this condition matches a record
-- between dst and src, an UPDATE is performed. Otherwise, an INSERT is
-- performed
)
WHEN MATCHED THEN UPDATE
SET dst."GLIST_VALUE_ID" = src."GLIST_VALUE_ID"
WHEN NOT MATCHED THEN INSERT ("GLIST_VALUE_ID", "USER_ID", "SESSION_ID")
VALUES (NULL, 1, 123456);
这只是为了给你一个想法。我不太确定你想要详细实现的是什么,所以我省略了子查询和条件
答案 1 :(得分:-1)
我发现这个查询也可以工作:
UPDATE "BMAN_TP1"."CELLS_GLIST"
SET "GLIST_VALUE_ID" = (
SELECT "GLIST_VALUE_ID"
FROM "BMAN_TP1"."GLISTS_VALUES_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_ID"=67)
AND ("GLIST_VALUE_DESC" = (
SELECT "GLIST_VALUE_DESC"
FROM "BMAN_TP1"."GLISTS_VALUES_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_VALUE_ID"="CELLS_GLIST"."GLIST_VALUE_ID")
))
),
"SESSION_ID" = 123456,
"USER_ID" = 1
WHERE EXISTS (......)
但是,它表现得非常快......我怀疑我错过了什么......