Oracle,使用预准备语句在一个查询中更新多行

时间:2016-04-06 07:58:25

标签: sql oracle sql-update prepared-statement

我知道您可以使用

在查询中插入多行
  INSERT ALL
  INTO mytable (column1, column2, column_n) VALUES (?,?,?)
  INTO mytable (column1, column2, column_n) VALUES (?,?,?)
  INTO mytable (column1, column2, column_n) VALUES (?,?,?)
  SELECT * FROM dual;

是否有基于值的更新方式?所以它会像

  UPDATE ALL
  SET mytable (column1, column2, column_n) VALUES (?,?,?)
  SET mytable (column1, column2, column_n) VALUES (?,?,?)
  SET mytable (column1, column2, column_n) VALUES (?,?,?)
  WHERE ID= ?
  SELECT * FROM dual;

例如我有一个评论表

       reviewid      bookid     authorname   authoremail
          1            1           peter        wdwdd
          2            1           jane         dwdwdw
          3            1           mary         dwdw

是否可以进行多次更新,其中bookid =" 1" ?

1 个答案:

答案 0 :(得分:0)

您可以使用MERGE

MERGE INTO reviewtable r
USING ( SELECT 1 AS reviewid,
               1 AS bookid,
               'peter smith' AS name,
               'p.smith@email' AS email
        FROM   DUAL
        UNION ALL
        SELECT 2, 1, 'jane blogs', 'j.blogs@email' FROM DUAL
        UNION ALL
        SELECT 3, 1, 'mary adams', 'm.adams@email' FROM DUAL
      ) src
ON    ( r.bookid = src.bookid AND r.reviewid = src.reviewid )
WHEN MATCHED THEN UPDATE
  SET authorname  = src.name,
      authoremail = src.email;