如何更新pl / sql

时间:2017-02-24 18:03:14

标签: oracle

我尝试更新查询中每一行的表中的列。我的代码是:

UPDATE CUSTOM_ISR_ASSET_DETAILS SET COUNTSYSTEMASSETS = ( Select  Count(PARENT_ID) as COUNTSYSTEMASSETS 
                              from PM_ENTITY 
                              inner join
                                (SELECT 
                                pm_entity.PM_ENTITY_ID,
                                response_text.response_text
                                FROM pm_entity 
                                INNER JOIN response_text  
                                ON pm_entity.pm_entity_id=response_text.parent_id 
                                AND response_text.question_id = '000ZLIJCA0003MI82Z000UWLUTG000000P4V') TBL_StandardRollup
                              on PM_ENTITY.PM_ENTITY_ID = TBL_StandardRollup.PM_ENTITY_ID
                              WHERE (TBL_StandardRollup.response_text = 'Standard')
                              group by PARENT_ID); 

我想要做的是用每个父ID的计数更新每一行,但显然,这会返回> 1行,因此更新失败。所以,我不能用这个sql代码做到这一点。

如何执行此操作以便使用父ID计数更新每一行?

2 个答案:

答案 0 :(得分:1)

请尝试使用merge声明。这将是:

merge into CUSTOM_ISR_ASSET_DETAILS c
using (/*YOUR SUB_QUERY*/) d
on (d.response_text = c./*what column 'standard' come from*/)
when matched then update set COUNTSYSTEMASSETS = d.COUNTSYSTEMASSETS;

我无法定义什么是连接条件。我想在值'standard'来自的列上。

答案 1 :(得分:0)

您有SELECT COUNT(x) GROUP BY x我认为您需要的是关联该查询并让它只返回一行:

UPDATE CUSTOM_ISR_ASSET_DETAILS
SET COUNTSYSTEMASSETS = (
    SELECT  Count(PARENT_ID)
    FROM whatever
    WHERE whatever.PARENT_ID = CUSTOM_ISR_ASSET_DETAILS.PARENT_ID ) ;

这样子查询只返回与您要更新的行对应的一行。