简体   繁体   English

Oracle Sql一次更新多个记录吗?

[英]Oracle Sql update multiple records at once?

Is it possible to Update multiple records in one sql query? 是否可以在一个sql查询中更新多个记录?
so update the status of all printers with id A and ip 1.1.1.1 to working (true), and all other as not working (false) 因此,将所有ID为A和ip 1.1.1.1的打印机的状态更新为工作(true),将所有其他打印机的状态更新为(false)
So basically combining these two query together into one query: 因此,基本上将这两个查询组合在一起成为一个查询:

update printers set status = true ,row_update_date =sysdate where printer id = 'A' and printer ip = '1.1.1.1'
update printers set status = false ,row_update_date =sysdate where printer id = 'A' and printer ip != '1.1.1.1'

table structure: 表结构:

printers table:

printer ID,printer ip, status,row_update_date 
A         ,1.1.1.1   ,
A         ,1.1.1.2   ,
A         ,1.1.1.3   ,
A         ,1.1.1.4   ,
B         ,1.1.2.1   ,
B         ,1.1.2.2   ,

UPDATE 更新
I forgot row_update_date ! 我忘了row_update_date!

Alternatively, you can use decode for Oracle's SQL for switch-case type statements 或者,您可以decode Oracle SQL的decode用于switch-case类型语句

update printers 
   set status = decode(printer_IP,'1.1.1.1','true','false'),
       row_update_date = sysdate
 where printer_ID = 'A';

You can try to UPDATE with CASE WHEN 您可以尝试使用CASE WHEN UPDATE

update printers 
set status = (CASE WHEN  printer ip = '1.1.1.1' 
                        THEN true
                   WHEN  printer ip != '1.1.1.1' 
                        THEN false 
              END)
WHERE printer id = 'A'

You could use CASE : 您可以使用CASE

update printers 
set status = CASE WHEN printer ip = '1.1.1.1' THEN true ELSE false END
    ,row_update_date = SYSDATE
where printer id = 'A' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM