根据sql-> oracle中的其他表更新表列

时间:2013-03-19 15:30:44

标签: sql oracle

这是我到目前为止所拥有的

BEGIN
             UPDATE t1
                set t1.LANGUAGE_ID = (case NETWORK.NID
                        when 11 then 10
                        else 7
                        end)
                FROM PROFILE, NETWORK where
                PROFILE.STATUS in ('A','U','S','H','I')
                AND PROFILE.MID = t1.MID
                AND NETWORK.MID = t1.MID
                AND t1.IS_DELETED = 'N'
END;

获取错误:

"ORA-06550: line 7, column 21:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 2, column 18:
PL/SQL: SQL Statement ignored
ORA-06550: line 12, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   ( begin case declare end exception exit for goto if loop mod
   null pragma raise return select update while with
   <an identifier> <a double-quoted"

我想在t1.is_deleted ='N'时更新t1,同样在配置文件中间有状态AUSHI。所有表都有一列中。任何想法为什么我得到这个错误?

2 个答案:

答案 0 :(得分:1)

UPDATE语句在Oracle中没有FROM子句。

听起来你想要一个相关的更新

UPDATE t1
   SET t1.language_id = (SELECT (CASE WHEN network.nid = 11 
                                      THEN 10
                                      ELSE 7
                                  END)
                           FROM profile,
                                network
                          WHERE profile.status IN ('A','U','S','H','I')
                            AND profile.mid = t1.mid
                            AND network.mid = t1.mid
                            AND t1.is_deleted = 'N');

如果相关子查询没有返回任何行,则会将t1设置language_id的每一行更新为NULL。如果您只想更新t1中子查询返回匹配行的行

UPDATE t1
   SET t1.language_id = (SELECT (CASE WHEN network.nid = 11 
                                      THEN 10
                                      ELSE 7
                                  END)
                           FROM profile,
                                network
                          WHERE profile.status IN ('A','U','S','H','I')
                            AND profile.mid = t1.mid
                            AND network.mid = t1.mid
                            AND t1.is_deleted = 'N')
  WHERE EXISTS( SELECT 1
                  FROM profile,
                       network
                 WHERE profile.status IN ('A','U','S','H','I')
                   AND profile.mid = t1.mid
                   AND network.mid = t1.mid
                   AND t1.is_deleted = 'N');

答案 1 :(得分:1)

@Justin提供的解决方案效果很好,但如果您的表很大,那么请考虑使用MERGE。使用提供的查询,您将为表t1的每一行运行嵌套的SELECT查询一次。

我发现此链接非常有用:8 Bulk Update Methods Compared