从另一个表更新多个列 - 需要Oracle格式

时间:2013-05-08 14:17:01

标签: sql-update oracle9i

我有一个在SQL Server中使用的脚本,但我需要将其转换为Oracle格式。有人可以帮忙吗?

    UPDATE PERSONS P SET 
        P.JOBTITLE=TE.JOBTITLE,
        P.LAST_NAME=TE.LAST_NAME,
        P.FIRST_NAME=TE.FIRST_NAME,
        P.DBLOGIN_ID=TE.DBLOGIN_ID,
        P.EMAIL_ID=TE.EMAIL_ID,
        P.USERLEVEL=TE.USERLEVEL,
        P.FACILITY_ID=TE.FACILITY_ID,
        P.SUPERVISOR=TE.SUPERVISOR,
        P.DEPARTMENT=TE.DEPARTMENT,
        P.WINLOGINID=TE.WINLOGINID
   FROM TEMP_ECOLAB_PERSONS TE
   WHERE P.PERSON=TE.PERSON;

- 从下面的文章中我得出以下声明。不幸的是,它仍然无效:

  UPDATE (SELECT P.JOBTITLE, P.LAST_NAME, P.FIRST_NAME, P.DBLOGIN_ID, P.EMAIL_ID,
        P.USERLEVEL, P.FACILITY_ID, P.SUPERVISOR, P.DEPARTMENT,
        TE.JOBTITLE, TE.LAST_NAME, TE.FIRST_NAME, TE.DBLOGIN_ID, TE.EMAIL_ID,
        TE.USERLEVEL, TE.FACILITY_ID, TE.SUPERVISOR, TE.DEPARTMENT
     FROM PERSONS P, TEMP_ECOLAB_PERSONS TE WHERE P.PERSON=TE.PERSON)
  SET 
     P.JOBTITLE=TE.JOBTITLE,
     P.LAST_NAME=TE.LAST_NAME,
     P.FIRST_NAME=TE.FIRST_NAME,
     P.DBLOGIN_ID=TE.DBLOGIN_ID,
     P.EMAIL_ID=TE.EMAIL_ID,
     P.USERLEVEL=TE.USERLEVEL,
     P.FACILITY_ID=TE.FACILITY_ID,
     P.SUPERVISOR=TE.SUPERVISOR,
     P.DEPARTMENT=TE.DEPARTMENT; 

3 个答案:

答案 0 :(得分:16)

UPDATE PERSONS P
   SET (jobtitle, 
        last_name, 
        first_name, 
        dblogin_id, 
        email_Id, 
        userlevel, 
        facility_id, 
        supervisor, 
        department, 
        winloginid) = (select jobtitle, 
                              last_name, 
                              first_name, 
                              dblogin_id, 
                              email_Id, 
                              userlevel, 
                              facility_id, 
                              supervisor,  
                              department, 
                              winloginid
                        from  TEMP_ECOLAB_PERSONS TE
                       where TE.PERSON=P.PERSON);

请注意,如果人员中存在不在temp_ecolab_persons中的其他行,则person表中的这些额外行将设置为null(或者可能导致语句失败,上面的更新不会出现空约束错误,因此,如果在这种情况下,您可能还需要在update语句中使用where子句来限制这些,例如,如果我知道email_id字段填充在某些记录上而不是其他记录上,我可以将更新限制为这些行,如下所示

UPDATE PERSONS P
  SET (jobtitle, 
       last_name, 
       first_name, 
       dblogin_id, 
       email_Id, 
       userlevel, 
       facility_id, 
       supervisor, 
       department, 
       winloginid) = (select jobtitle, 
                             last_name, 
                             first_name, 
                             dblogin_id, 
                             email_Id, 
                             userlevel, 
                             facility_id, 
                             supervisor,  
                             department, 
                             winloginid
                       from  TEMP_ECOLAB_PERSONS TE
                      where TE.PERSON=P.PERSON)
   WHERE email_id is null;

答案 1 :(得分:11)

我就是这样做的。它可能不是最好的表现,但它确实有效。

MERGE INTO PERSONS_TMP PT
USING ( 
    SELECT P.PERSON, P.JOB_TITLE, P.FIRST_NAME, P.LAST_NAME, P.FACILITY_ID 
    FROM PERSONS P) TMP
ON (PT.PERSON = TMP.PERSON)
WHEN MATCHED THEN 
UPDATE SET 
    PT.FACILITY_ID = TMP.FACILITY_ID, 
    PT.JOB_TITLE = TMP.JOB_TITLE,
    PT.FIRST_NAME = TMP.FIRST_NAME,
    PT.LAST_NAME = TMP.LAST_NAME;

上面的脚本将使用PERSONS表中的数据更新PERSONS_TMP表中的信息。我相信你的情况,你想要反过来。因此,请确保在运行脚本之前进行必要的更改。

如果您需要插入新记录(如果不存在),您可以在上面的SQL中添加“WHEN NOT MATCHED THEN ....”子句。

答案 2 :(得分:-1)

或者我们可以使用

Where exists (select 1  from  TEMP_ECOLAB_PERSONS TE
                      where TE.PERSON=P.PERSON)