views:

54

answers:

2

Hi! Can you tell me how to update multiple rows in oracle as when I fired the update statement it gave me the following error

UPDATE  BI_BSELATEST_LATESTPRICESTEST
SET PREVIOUS_DAY_CLOSE =
    (SELECT  DISTINCT aa.DLYPRICE_CLOSE 
     FROM  DATAFEED_EQTY.FEED_DLYPRICE aa  ,
           (
           SELECT  a.sc_code ,  MAX(a.DLYPRICE_DATE) as max_date
           from    DATAFEED_EQTY.FEED_DLYPRICE a,BI_BSELATEST_LATESTPRICES b
           where   a.SC_CODE = b.SC_CODE
           and    a.ST_EXCHNG = 'BSE'
           and    a.DLYPRICE_DATE <   b.upd_time 
           group by a.sc_code
         ) bb
    WHERE  aa.SC_CODE =  bb.sc_code
    and    aa.DLYPRICE_DATE = max_date)

ORA-01427: single-row subquery returns more than one row

Thanks in advance

+2  A: 

A subquery in your statement is wrong. You left off either a WHERE or FIRST clause, and now it's returning multiple values when it shouldn't.

You're basically trying to say PREVIOUS_DAY_CLOSE should be multiple values at the same time. I'm guessing you left off a WHERE clause on your subselect, which would link the results of that subquery to the particular row you're trying to update. Something like (note the bolded line):

UPDATE  BI_BSELATEST_LATESTPRICESTEST
SET PREVIOUS_DAY_CLOSE =
    (SELECT  DISTINCT aa.DLYPRICE_CLOSE 
     FROM  DATAFEED_EQTY.FEED_DLYPRICE aa  ,
           (
           SELECT  a.sc_code ,  MAX(a.DLYPRICE_DATE) as max_date
           from    DATAFEED_EQTY.FEED_DLYPRICE a,BI_BSELATEST_LATESTPRICES b
           where   a.SC_CODE = b.SC_CODE
           and    a.ST_EXCHNG = 'BSE'
           and    a.DLYPRICE_DATE <   b.upd_time 
           group by a.sc_code
         ) bb
    WHERE  aa.SC_CODE =  bb.sc_code
    and    aa.DLYPRICE_DATE = max_date

   AND bb.sc_code = BI_BSELATEST_LATESTPRICESTEST.sc_code

    )

Although, I'll be honest, I'm not exactly sure what you're trying to do with this query.

lc
m i want to update records in PREVIOUS_DAY_CLOSE field in BI_BSELATEST_LATESTPRICESTEST table with the values i get in select statement hope this info helps you..
devang
@devang - Right, so for a certain given row of `BI_BSELATEST_LATESTPRICESTEST`, *which* value that you get from the select statement do you want. One column from one row can only have one value at any given time, right?
lc
yes correct i get only when value for one column..but still it gives me the same error ORA-01427: single-row subquery returns more than one row.
devang
@devang - I don't think you're quite understanding. Take your subselect out and try running it by itself and see what result*s* you get, because you'll *surely* get more than one row.
lc
@lc- i tried running the select and sub select query it gives me some 1000 rows now can u tell me how to update these rows in the PREVIOUS_DAY_CLOSE column in BI_BSELATEST_LATESTPRICESTEST table.
devang
@lc- in oracle it does not allow multiple updates at a time as i tried updating by defining the value implicitly and it worked..
devang
@devang - Ok I'm not sure how else to word it, but basically one column in *one row* in `BI_BSELATEST_LATESTPRICESTEST` **cannot contain 1000 values at the same time**. It's like saying I want x to equal 2, 3, and 4 at the same time.So the question is: *As it goes through and updates each row, for a given existing row in `BI_BSELATEST_LATESTPRICESTEST`, which one of the 1000 rows do you want to set `PREVIOUS_DAY_CLOSE` to?*
lc
@lc- i got what you saying i want to set the 1st highest value as my PREVIOUS_DAY_CLOSE value..
devang
@lc- i even tried where exists clause but it also doesnt seem to work here..:(
devang
@devang Then instead of `DISTINCT`, maybe you mean `MAX`? As in `(SELECT MAX(aa.DLYPRICE_CLOSE)...`. But you still don't have any link between `aa`/`bb` and `BI_BSELATEST_LATESTPRICESTEST`, as I've suggested, so I'm afraid all rows are going to be replaced with the same max value...Could you post some example data and what you want to happen??
lc
A: 

I think what you want is this:

UPDATE  BI_BSELATEST_LATESTPRICESTEST b
SET PREVIOUS_DAY_CLOSE =
    (SELECT  DISTINCT aa.DLYPRICE_CLOSE 
     FROM  DATAFEED_EQTY.FEED_DLYPRICE aa  ,
           (
           SELECT  a.sc_code ,  MAX(a.DLYPRICE_DATE) as max_date
           from    DATAFEED_EQTY.FEED_DLYPRICE a
           where   a.SC_CODE = b.SC_CODE
           and    a.ST_EXCHNG = 'BSE'
           and    a.DLYPRICE_DATE <   b.upd_time 
           group by a.sc_code
         ) bb
    WHERE  aa.SC_CODE =  bb.sc_code
    and    aa.DLYPRICE_DATE = max_date)

This removes BI_BSELATEST_LATESTPRICESTEST from the sub-query and instead tells the database to use the columns from the table that it is updating to filter the sub-query. As you had written it, the database had no way of knowing how to correlate the columns from the sub-query to the table being updated.

Allan