Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Problems on update

$
0
0
I tried updating the column name projectid in actualhrs_staging table with the column projectid in VW_PC_PBEXTRACT view.
i tried these methods but failed with all.

1)
Code:

update actualhrs_staging ahs
set projectid = (select projectid from VW_PC_PBEXTRACT as clarproj
                where clarproj.clarityid = ahs.clarityid)
where loaddate = '2012-07-30'

the following error occurs :
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO statement is more than one row.

2)
Code:

merge into actualhrs_staging ahs
using (select projectid, clarityid
        from VW_PC_PBEXTRACT
        where clarityid in
                                (select distinct clarityid
                                from actualhrs_staging
                                where loaddate ='2012-07-30')) new
on ahs.clarityid = new.clarityid
when matched then
 update set
        ahs.projectid = new.projectid
        else ignore

following error occurs when i tried above sql :
SQL0788N The same row of target table "RPD.ACTUALHRS_STAGING

3)
In this method i tried using for loop in stored procedure so that i can update one row at a time, but this also failed.
I am not getting any error with this method, but instead getting wrong result.
Here's a stored procedure i created.
Code:

CREATE PROCEDURE updateclaritytable

BEGIN
DECLARE PROJID INTEGER;
DECLARE PROJTEAMID INTEGER;
DECLARE STEAMID INTEGER;
DECLARE SUPPCODE VARCHAR(50);
DECLARE CLAR VARCHAR(50);
SET CURRENT SCHEMA RPD;

FOR v_row AS select projectid, clarityid, projectteamid,siteteam,suppliercode
                    from VW_PC_PBEXTRACT as clarproj
                    where clarproj.clarityid in
                                                    (select distinct clarityid
                                                      from actualhrs_staging
                                                      where loaddate = '2012-07-30')
    DO                         
        SET PROJID = v_row.projectid;
        SET PROJTEAMID = v_row.projectteamid;
        SET STEAMID = v_row.siteteam;
        SET SUPPCODE = v_row.suppliercode;
            SET CLAR = v_row.clarityid;
                             
END FOR;
        UPDATE ACTUALHRS_STAGING 
        SET projectid = PROJID
        where loaddate = '2012-07-30'
        and clarityid = CLAR;   
END

After successful calling of the stored procedure, i found that all the rows are being updated with single value i.e. projectid = 100002469. This is not a desired result as every row should have different projectid with the sentinel value is actualhrs_staging.clarityid = VW_PC_PBEXTRACT.clarityid.

Thanks any help will be appreciated.

Jas

Viewing all articles
Browse latest Browse all 13329

Trending Articles