更新多行,在1个oracle过程中使用许多不同的值(Update multiple lines, with many different values in 1 oracle procedure)
例如,我需要更新3行。 我正在运行UPDATE语句3次更新每一行:
UPDATE table SET col1 = 'a' where col2 = '1';
UPDATE table SET col1 = 'b' where col2 = '2';
UPDATE table SET col1 = 'c' where col2 = '3';
那么有没有办法让它更快,谢谢
example, i need update 3 rows. I'm doing is running UPDATE Statement 3 time to update each row:
UPDATE table SET col1 = 'a' where col2 = '1';
UPDATE table SET col1 = 'b' where col2 = '2';
UPDATE table SET col1 = 'c' where col2 = '3';
So is there a way to make it faster, thank you
原文:https://stackoverflow.com/questions/38493924
更新时间:2019-12-21 07:05
最满意答案
三次update是一种非常合理的方法。 你也可以这样做:
update table
set col1 = (case when col2 = 1 then 'a'
when col2 = 2 then 'b'
when col2 = 3 then 'c'
else col1
end)
where col2 in (1, 2, 3);
Three updates are a very reasonable approach. You could also do:
update table
set col1 = (case when col2 = 1 then 'a'
when col2 = 2 then 'b'
when col2 = 3 then 'c'
else col1
end)
where col2 in (1, 2, 3);
2016-07-21
相关问答
类似于GurV的答案(因为他打了30秒......),你也可以使用PL / SQL对象类型。 如果不需要在SQL中引用类型,则不需要CREATE TYPE语句。 --Package Spec
CREATE OR REPLACE PACKAGE TEST AS
TYPE id_table_type IS TABLE OF NUMBER;
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id
...
有很多方法来做字符串聚合,但最简单的是用户定义的函数。 尝试这样一种不需要功能的方式。 作为一个说明,没有功能没有简单的方法。 这是没有自定义功能的最短路由:(它使用ROW_NUMBER()和SYS_CONNECT_BY_PATH函数) SELECT questionid,
LTRIM(MAX(SYS_CONNECT_BY_PATH(elementid,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS elements
...
有一些类似“表值参数”的东西可以帮助您在DB中存储多行 请参阅此。 这可能会给你一些帮助 In Java, We can use the STRUCT Class for Oracle Objects. For more details, check the below link https://docs.oracle.com/cd/B10501_01/java.920/a96654/oraoot.htm
如果您对变量有价值,则将其作为输入参数 CREATE OR REPLACE PROCEDURE TEST_CHK
(IN_L_ID OUT NUMBER, S_NAME VARCHAR2(50),R_ID VARCHAR2(50) )
AS
CURSOR STA_CHECK IS
Select NAME, ID FROM KPI_LOG where NAME = S_NAME and ID =R_ID;
BEGIN
...
END TEST_CHK;
...
三次update是一种非常合理的方法。 你也可以这样做: update table
set col1 = (case when col2 = 1 then 'a'
when col2 = 2 then 'b'
when col2 = 3 then 'c'
else col1
end)
where col2 in (1, 2,
...
作为参数传入存储过程的值将始终可用于存储过程。 使用自治事务声明过程无关紧要。 在自治事务中运行的代码无法看到调用事务所做的更改。 10次中有9次,当人们描述看到他们期望的数据的问题时,这就是问题的根源。 如果您的存储过程正在执行除了向日志表写入内容之外的任何操作,我将非常谨慎地使用自治事务。 如果您使用自动事务处理除日志记录以外的任何事情,您几乎肯定会错误地使用它们。 而且你可能会引入一系列与竞争条件和交易完整性相关的错误。 Values passed in to a stored proced
...
您不需要使用过程,只需使用sql脚本即可解决您的问题。 update medicine m
set m.PDF_KUB_PATH = (select p.PDF_KUB_PATH from parsed_ilac_rehberi p where m.BARCODE = p.BARCODE);
You dont need to use a procedure, you can solve your question just with sql script. update medicin
...
UPDATE CUSTOMER_INFO
SET CUSTOMER_TYPE = NVL(CustomerType, CUSTOMER_TYPE),
CUSTOMER_NAME = NVL(CustomerName, CUSTOMER_NAME),
CUSTOMER_GENDER = NVL(CustomerGender, CUSTOMER_GENDER),
CUSTOMER_BIRTHDAY = NVL(CustomerBirt
...
你能做的就是同时提交大量的工作。 请参见示例28-2在单个事务中创建一组轻量级作业 这将填充pl / sql表,其中包含您要在一个tx中同时提交的所有作业。 一旦提交(启用)它们,它们就会开始运行,系统可以处理多少,或者资源管理器计划允许的数量。 轻量级作业的开销非常小/轻。 I would like to close this question. DBMS_SCHEDULER as well as DBMS_JOB (though DBMS_SCHEDULER is preferred) can
...
另一种方法: merge into
tester
using (
select 1 id,'0123456785' employee_phone_number from dual union all
select 2 id,'0123456786' employee_phone_number from dual) new_values
on (
tester.id = new_values.id)
when matched then update
set employee_p
...