2015-09-25 67 views
0

我有這條SQL語句,但我不知道如何在Oracle中編寫它(Oracle不支持多表更新):我該如何寫這個查詢從MySQL(SQL)到Oracle(SQL)

UPDATE sscanner.sis_emp_card ec, sscanner.sis_cards c, sscanner.sis_employees e 
SET e.firstname='x', e.lastname='y', e.car_number=NULL, c.card_number='12345', e.email='[email protected]', ec.active='1' 
WHERE ec.id_card = c.id_card AND ec.id_emp=e.id_employee AND id=1; 

sis_cards:id_card(PK)..

sis_employees:id_employee(PK)..

sis_emp_card:ID(PK),id_card(FK)引用sis_cards,id_employee(FK)引用sis_employees

回答

1

我知道這個問題已經回答了,但對於一些更新與Oracle語法。最後一個是更新連接視圖。

UPDATE sscanner.sis_employees e 
     SET e.firstname='x', 
      e.lastname='y', 
      e.car_number=NULL, 
      e.email = '[email protected]' 
    WHERE EXISTS (
     SELECT * 
     FROM sscanner.sis_emp_card ec, sscanner.sis_cards c 
     WHERE ec.id_card = c.id_card AND ec.id_employee=e.id_employee AND ec.id=1); 

    UPDATE sscanner.sis_cards c 
     SET c.card_number = '12345' 
    WHERE EXISTS(
     SELECT * 
     FROM sscanner.sis_emp_card ec, sscanner.sis_employees e 
     WHERE c.id_card = ec.id_card AND ec.id_employee=e.id_employee AND ec.id=1); 

    UPDATE (
     SELECT e.firstname, e.lastname, e.car_number, e.email, 
     c.card_number, ec.active 
     FROM sscanner.sis_emp_card ec, sscanner.sis_cards c, sscanner.sis_employees e 
     WHERE ec.id_card = c.id_card AND ec.id_employee=e.id_employee AND ec.id=1 
     ) ec SET 
     ec.active='1'; 
1

嘗試通過一個更新一個,像婁代碼

begin 
    for R in (select e.rowid ERecId, c.rowid CRecId, ec.rowid ECRecId 
       from sscanner.sis_emp_card ec, sscanner.sis_cards c, 
        sscanner.sis_employees e 
       where ec.id_card = c.id_card 
       and ec.id_emp = e.id_employee 
       and id = 1) loop 

     update sscanner.sis_employees 
     set firstname = 'x', 
      lastname = 'y', 
      car_number = null, 
      email = '[email protected]' 
     where rowid = R.ERecid; 

     update sscanner.sis_cards 
     set card_number = '12345' 
     where rowid = R.CRecId; 

     update sscanner.sis_emp_card ec 
     set ec.active = '1' 
     where rowid = R.ECRecId; 

    end loop; 
end; 
+0

工程就像一個魅力。 –