2013-07-31 94 views
0

嘗試將Python中的列表插入到Oracle數據庫中。我想從每個列表中插入4條數據到Oracle中,如果有3條數據條目匹配,我想更新。如果它們不匹配,我想插入。Oracle合併到聲明中

Equip_ID 
Parameter_Name 
Parameter_Value 
Customer 

因此,對於這一個項目的,如果Equip_ID,PARAMETER_NAME,和客戶在數據庫中已匹配的條目,然後我想更新的參數值。如果它們不匹配,那麼我想將所有數據插入到數據庫中。這是我迄今爲止的,但似乎沒有工作。

MERGE INTO dbName.tableName 
    USING '26522931','Customer','Para001','99999' 
    ON (tableName.EQUIPMENT_NODE_ID = '26522931' AND tableName.STREAM_NAME = 'Customer' AND tableName.PARAMETER_NAME = 'Para000') 
WHEN MATCHED THEN 
    UPDATE SET tableName.PARAMETER_VALUE = '99999' 
WHEN NOT MATCHED THEN 
    INSERT (EQUIPMENT_NODE_ID,STREAM_NAME,PARAMETER_NAME,PARAMETER_VALUE) VALUES('26522931','Customer','Para000','99999') 

回答

1

我的猜測是你想要這樣的東西。如果您沒有其他表格用作源,則需要從DUAL中選擇數據。

MERGE INTO dbName.tableName 
    USING (select '26522931' equipment_node_id, 
       'Customer' stream_name, 
       'Para001' parameter_name, 
       '99999' parameter_value 
      from dual) src 
    ON (tableName.EQUIPMENT_NODE_ID = src.EQUIPMENT_NODE_ID 
    AND tableName.STREAM_NAME = src.STREAM_NAME 
    AND tableName.PARAMETER_NAME = src.PARAMETER_NAME) 
WHEN MATCHED THEN 
    UPDATE SET tableName.PARAMETER_VALUE = src.parameter_value 
WHEN NOT MATCHED THEN 
    INSERT (EQUIPMENT_NODE_ID, 
      STREAM_NAME, 
      PARAMETER_NAME, 
      PARAMETER_VALUE) 
    VALUES(src.EQUIPMENT_NODE_ID, 
      src.STREAM_NAME, 
      src.PARAMETER_NAME, 
      src.PARAMETER_VALUE)