我正在尝试使用WITH子句在Oracle中使用合并,但由于某种原因我无法使其工作.我确信这是显而易见的,但我还没有看到它.
-- behold,the wonders of fake data WITH X AS ( SELECT 'moo' AS COW,'woof' AS CAT,(SELECT MAX( DECIBELS ) FROM ANIMALIA WHERE COW = 'moo' ) AS DECIBELS FROM DUAL ) MERGE INTO ANIMALIA D USING X WHEN MATCHED THEN UPDATE SET D.COW = X.COW;
编辑
我实际上发现了如何处理这个问题(在我提交问题之前),但我认为,因为我花了很长时间才找到答案,希望将这个问题留下来意味着下一个人会发现它不是那么很多时间.
我将在一天左右的时间内发布答案,但如果其他人在同一时间发布,他们将获得积分.
您不能在SELECT语句中的任何位置使用WITH子句.
See the documentation here.:
You can specify this clause in any top-level SELECT statement and in
most types of subqueries.
所以,你可以做这样的事情(11g测试):
MERGE INTO animalia d USING (WITH X AS (SELECT 'moo' AS COW,(SELECT MAX( DECIBELS ) FROM ANIMALIA WHERE COW = 'moo' ) AS DECIBELS FROM DUAL ) SELECT * FROM X) q ON (1 = 1) WHEN MATCHED THEN UPDATE SET d.cow = q.cow||' and more';