SQL Update

Hello,
I'm trying to do a simple update, but I'm getting error

Select Price from  priceszoneweight  Where PricePlanID in (5122) and WeightFrom = 40

Update priceszoneweight set Price = (Select Price from  priceszoneweight  Where PricePlanID in (5122) and WeightFrom = 40) Where PricePlanID in (5122) and WeightFrom = 41

Error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Can you please help,
Thank you,
W.E.BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

try
Update priceszoneweight set Price =  (Select Price from  priceszoneweight  Where PricePlanID = 5122 and WeightFrom IN(40, 41))
0
W.E.BAuthor Commented:
Hello,
No, this will not work,
This updates all tables.

I have to limit the update to
Where PricePlanID in (5122) and WeightFrom = 41
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
When assigning the results of a subquery (any subquery here) to a single value (Price), it is required that the subquery return a single value.  More than one value and it generates the error message you received.
Update priceszoneweight 
set Price = (any subquery here) 

Open in new window

So just for kicks and giggles, run the T-SQL you have in the subquery and see how many rows are returned.
Select Price from  priceszoneweight  Where PricePlanID in (5122) and WeightFrom = 40

Open in new window

Chances are you'll need either SELECT DISTINCT vs. SELECT, or there are duplicate rows being returned that you'll need to resolve, either because of a data entry error, or the WHERE clause needs more criteria to ensure only one row is returned.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

W.E.BAuthor Commented:
HI Jim,
this is correct, I have more than one value. (Because I have more than one cell to update)
even when I tried the update with a distinct, I still get the same error.

Update priceszoneweight set Price = (Select Distinct Price from  priceszoneweight  Where PricePlanID in (5122) and WeightFrom = 40) Where PricePlanID in (5122) and WeightFrom = 41
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Traditionally you update with the MIN, MAX or AVG value if it is not unique. And, as Jim said, you have to have more than one row returned for getting that error message.
0
Éric MoreauSenior .Net ConsultantCommented:
if you query
Select Price from  priceszoneweight  Where PricePlanID in (5122) and WeightFrom = 40

Open in new window


returns more than one row, which Price should be taken to update the row you want to update?
0
W.E.BAuthor Commented:
Ok, I think I understand the need of another row,
But I still can't get my head to get it to Update.
I think I need a join or something,

If I run
Select PricePlanID,ZoneFromID,ZoneToID,WeightFrom,WeightTo,Price
From  priceszoneweight  Where PricePlanID in (5205) and WeightFrom = 50 and ZoneFromID = 1852
UNion all
Select PricePlanID,ZoneFromID,ZoneToID,WeightFrom,WeightTo,Price
From  priceszoneweight  Where PricePlanID in (5205) and WeightFrom = 51 and ZoneFromID = 1852

I get attached sample results.

What I need to do is have the prices of Weight From = 51 , to be equal to Weight From = 50

Example:
WeightFrom      = 50
PricePlanID      ZoneFromID      ZoneToID      WeightFrom      WeightTo      Price
5205              1852              1852                  50                51                     24.25

Then WeightFrom       = 51 should be the same
PricePlanID      ZoneFromID      ZoneToID      WeightFrom      WeightTo      Price
5205              1852              1852                  51                   99999          24.25

Thanks,
Sample.xlsx
0
Éric MoreauSenior .Net ConsultantCommented:
From what I see in your sample, for a single PricePlanID and WeightFrom you have multiple rows/Price.

You can't update one row with the result of multiple rows.

It looks like ZoneToID make the rows unique (for a PricePlanID and WeightFrom ). Can it be added to your query?
0
W.E.BAuthor Commented:
I have a lot of ZoneToId to add.

the Common between is
if
ZoneFromId = ZoneFromId
WeightFrom= WeightFrom (+1)
ZoneToID = ZoneToID

Or
ZoneFromId.WeightFrom.ZoneToID = ZoneFromId.WeightFrom (+1) .ZoneToID
Then Update price to ZoneFromId.WeightFrom.ZoneToID.Price

I know this is BY FAR not a solution, But this is the logic of it to Update.

thanks,
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You certainly do not want to just update the "same" rows for WeightFrom 51 with those of 50. Of course a join using ZoneToID would help, but I do not believe the price should be the same for weight below and starting with 51. There should be an added fee, no?

It always helps me to first write an updatable SELECT containing all source and destination data. That would be
select srcPrice=s.Price, d.*
from priceszoneweight  s
join priceszoneweight  d on s.PricePlanID = d.PricePlanID and s.ZoneFromID = d.ZoneFromID and s.ZoneToID = d.ZoneToID
where s.WeightFrom = 50 and d.WeightFrom = 51

Open in new window

If that shows correct values, the UPDATE is easy:
UPDATE tbl
set price = srcPrice
from (
 /* above SELECT */
) tbl

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Having a fixed offset in above SELECT is easy, too, And I forgot to make sure s and d are not the same row (shame on me):
select srcPrice=s.Price, d.*
from priceszoneweight  s
join priceszoneweight  d on s.PricePlanID = d.PricePlanID and s.ZoneFromID = d.ZoneFromID and s.ZoneToID = d.ZoneToID
and s.WeightFrom = d.WeightFrom+1
where s.WeightFrom = 50

Open in new window

0
W.E.BAuthor Commented:
I added
and d.PricePlanID in (5122)

no rows found.

thanks
0
Anoo S PillaiCommented:
If my understanding is correct, following update will server your purpose.
UPDATE	TRGT
SET	TRGT.Price = SRC.Price
FROM	priceszoneweight SRC 
JOIN	priceszoneweight TRGT
ON	SRC.PricePlanID      = TRGT.PricePlanID
AND	SRC.ZoneFromID       = TRGT.ZoneFromID
AND	SRC.ZoneToID         = TRGT.ZoneToID
AND	SRC.WeightFrom + 1   = TRGT.WeightFrom

WHERE	SRC.PricePlanID  = 5205
AND	TRGT.PricePlanID = 5205

Open in new window


The where clause, you could modify as per your requirement. Probably a condition like "AND TRGT.Price = 0 AND TRGT.WeightTo = 99999 "  to where clause to update only the intended rows.

Hope this help.
Anoo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
Consider this select query, take note that I have deliberately joined s.WeightTo = d.WeightFrom
select
      d.price, s.Price as srcPrice
    , s.PricePlanID , d.PricePlanID 
    , s.ZoneFromID , d.ZoneFromID
    , s.ZoneToID , d.ZoneToID
    , s.WeightTo , d.WeightFrom
from priceszoneweight  s
join priceszoneweight  d 
    on s.PricePlanID = d.PricePlanID 
    and s.ZoneFromID = d.ZoneFromID
    and s.ZoneToID = d.ZoneToID
    and s.WeightTo = d.WeightFrom --<< note, important
where d.Price = 0
;

Open in new window

Then using that logic this update statement:
update priceszoneweight
set price = srcPrice
from priceszoneweight d
join (
      select
            s.Price as srcPrice
          , d.PricePlanID 
          , d.ZoneFromID
          , d.ZoneToID
          , d.WeightFrom
          , d.WeightTo
      from priceszoneweight  s
      join priceszoneweight  d 
          on s.PricePlanID = d.PricePlanID 
          and s.ZoneFromID = d.ZoneFromID
          and s.ZoneToID = d.ZoneToID
          and s.WeightTo = d.WeightFrom --<< note, important, dissimilar
      where d.Price = 0
      ) s
on s.PricePlanID = d.PricePlanID 
    and s.ZoneFromID = d.ZoneFromID
    and s.ZoneToID = d.ZoneToID
    and s.WeightTo = d.WeightTo     --<< note, important, now the same
    and s.WeightFrom = d.WeightFrom --<< note, important, now the same
;

select
*
from priceszoneweight
;

Open in new window

Produces this result:
| PricePlanID | ZoneFromID | ZoneToID | WeightFrom | WeightTo | Price |
|-------------|------------|----------|------------|----------|-------|
|        5205 |       1852 |     1852 |         50 |       51 | 24.25 |
|        5205 |       1852 |     1853 |         50 |       51 | 29.85 |
|        5205 |       1852 |     1854 |         50 |       51 | 29.85 |
|        5205 |       1852 |     1855 |         50 |       51 |  31.3 |
|        5205 |       1852 |     1856 |         50 |       51 | 34.85 |
|        5205 |       1852 |     1857 |         50 |       51 |  36.6 |
|        5205 |       1852 |     1858 |         50 |       51 | 37.45 |
|        5205 |       1852 |     1859 |         50 |       51 |  38.3 |
|        5205 |       1852 |     1860 |         50 |       51 |  38.6 |
|        5205 |       1852 |     1861 |         50 |       51 |  41.2 |
|        5205 |       1852 |     1862 |         50 |       51 | 41.85 |
|        5205 |       1852 |     1863 |         50 |       51 | 46.15 |
|        5205 |       1852 |     1864 |         50 |       51 |  48.2 |
|        5205 |       1852 |     1852 |         51 |    99999 | 24.25 |
|        5205 |       1852 |     1853 |         51 |    99999 | 29.85 |
|        5205 |       1852 |     1854 |         51 |    99999 | 29.85 |
|        5205 |       1852 |     1855 |         51 |    99999 |  31.3 |
|        5205 |       1852 |     1856 |         51 |    99999 | 34.85 |
|        5205 |       1852 |     1857 |         51 |    99999 |  36.6 |
|        5205 |       1852 |     1858 |         51 |    99999 | 37.45 |
|        5205 |       1852 |     1859 |         51 |    99999 |  38.3 |
|        5205 |       1852 |     1860 |         51 |    99999 |  38.6 |
|        5205 |       1852 |     1861 |         51 |    99999 |  41.2 |
|        5205 |       1852 |     1862 |         51 |    99999 | 41.85 |
|        5205 |       1852 |     1863 |         51 |    99999 | 46.15 |
|        5205 |       1852 |     1864 |         51 |    99999 |  48.2 |
        

Open in new window


details:
**MS SQL Server 2008 Schema Setup**:

    CREATE TABLE priceszoneweight  
        ([PricePlanID] int, [ZoneFromID] int, [ZoneToID] int, [WeightFrom] int, [WeightTo] int, [Price] decimal(12,3))
    ;
        
    INSERT INTO priceszoneweight  
        ([PricePlanID], [ZoneFromID], [ZoneToID], [WeightFrom], [WeightTo], [Price])
    VALUES
        (5205, 1852, 1852, 50, 51, 24.25),
        (5205, 1852, 1853, 50, 51, 29.85),
        (5205, 1852, 1854, 50, 51, 29.85),
        (5205, 1852, 1855, 50, 51, 31.3),
        (5205, 1852, 1856, 50, 51, 34.85),
        (5205, 1852, 1857, 50, 51, 36.6),
        (5205, 1852, 1858, 50, 51, 37.45),
        (5205, 1852, 1859, 50, 51, 38.3),
        (5205, 1852, 1860, 50, 51, 38.6),
        (5205, 1852, 1861, 50, 51, 41.2),
        (5205, 1852, 1862, 50, 51, 41.85),
        (5205, 1852, 1863, 50, 51, 46.15),
        (5205, 1852, 1864, 50, 51, 48.2),
        (5205, 1852, 1852, 51, 99999, 0),
        (5205, 1852, 1853, 51, 99999, 0),
        (5205, 1852, 1854, 51, 99999, 0),
        (5205, 1852, 1855, 51, 99999, 0),
        (5205, 1852, 1856, 51, 99999, 0),
        (5205, 1852, 1857, 51, 99999, 0),
        (5205, 1852, 1858, 51, 99999, 0),
        (5205, 1852, 1859, 51, 99999, 0),
        (5205, 1852, 1860, 51, 99999, 0),
        (5205, 1852, 1861, 51, 99999, 0),
        (5205, 1852, 1862, 51, 99999, 0),
        (5205, 1852, 1863, 51, 99999, 0),
        (5205, 1852, 1864, 51, 99999, 0)
    ;
    
**Query 1**:

    select
          d.price, s.Price as srcPrice
        , s.PricePlanID , d.PricePlanID 
        , s.ZoneFromID , d.ZoneFromID
        , s.ZoneToID , d.ZoneToID
        , s.WeightTo , d.WeightFrom
    from priceszoneweight  s
    join priceszoneweight  d 
        on s.PricePlanID = d.PricePlanID 
        and s.ZoneFromID = d.ZoneFromID
        and s.ZoneToID = d.ZoneToID
        and s.WeightTo = d.WeightFrom --<< note, important
    where d.Price = 0
    

**[Results][2]**:
    | price | srcPrice | PricePlanID | PricePlanID | ZoneFromID | ZoneFromID | ZoneToID | ZoneToID | WeightTo | WeightFrom |
    |-------|----------|-------------|-------------|------------|------------|----------|----------|----------|------------|
    |     0 |    24.25 |        5205 |        5205 |       1852 |       1852 |     1852 |     1852 |       51 |         51 |
    |     0 |    29.85 |        5205 |        5205 |       1852 |       1852 |     1853 |     1853 |       51 |         51 |
    |     0 |    29.85 |        5205 |        5205 |       1852 |       1852 |     1854 |     1854 |       51 |         51 |
    |     0 |     31.3 |        5205 |        5205 |       1852 |       1852 |     1855 |     1855 |       51 |         51 |
    |     0 |    34.85 |        5205 |        5205 |       1852 |       1852 |     1856 |     1856 |       51 |         51 |
    |     0 |     36.6 |        5205 |        5205 |       1852 |       1852 |     1857 |     1857 |       51 |         51 |
    |     0 |    37.45 |        5205 |        5205 |       1852 |       1852 |     1858 |     1858 |       51 |         51 |
    |     0 |     38.3 |        5205 |        5205 |       1852 |       1852 |     1859 |     1859 |       51 |         51 |
    |     0 |     38.6 |        5205 |        5205 |       1852 |       1852 |     1860 |     1860 |       51 |         51 |
    |     0 |     41.2 |        5205 |        5205 |       1852 |       1852 |     1861 |     1861 |       51 |         51 |
    |     0 |    41.85 |        5205 |        5205 |       1852 |       1852 |     1862 |     1862 |       51 |         51 |
    |     0 |    46.15 |        5205 |        5205 |       1852 |       1852 |     1863 |     1863 |       51 |         51 |
    |     0 |     48.2 |        5205 |        5205 |       1852 |       1852 |     1864 |     1864 |       51 |         51 |
**Query 2**:

    
    
    update priceszoneweight
    set price = srcPrice
    from priceszoneweight d
    join (
          select
                s.Price as srcPrice
              , d.PricePlanID 
              , d.ZoneFromID
              , d.ZoneToID
              , d.WeightFrom
              , d.WeightTo
          from priceszoneweight  s
          join priceszoneweight  d 
              on s.PricePlanID = d.PricePlanID 
              and s.ZoneFromID = d.ZoneFromID
              and s.ZoneToID = d.ZoneToID
              and s.WeightTo = d.WeightFrom --<< note, important, dissimilar
          where d.Price = 0
          ) s
    on s.PricePlanID = d.PricePlanID 
        and s.ZoneFromID = d.ZoneFromID
        and s.ZoneToID = d.ZoneToID
        and s.WeightTo = d.WeightTo     --<< note, important, now the same
        and s.WeightFrom = d.WeightFrom --<< note, important, now the same
    

**[Results][3]**:
**Query 3**:

    
    
    select
    *
    from priceszoneweight
    

**[Results][4]**:
    | PricePlanID | ZoneFromID | ZoneToID | WeightFrom | WeightTo | Price |
    |-------------|------------|----------|------------|----------|-------|
    |        5205 |       1852 |     1852 |         50 |       51 | 24.25 |
    |        5205 |       1852 |     1853 |         50 |       51 | 29.85 |
    |        5205 |       1852 |     1854 |         50 |       51 | 29.85 |
    |        5205 |       1852 |     1855 |         50 |       51 |  31.3 |
    |        5205 |       1852 |     1856 |         50 |       51 | 34.85 |
    |        5205 |       1852 |     1857 |         50 |       51 |  36.6 |
    |        5205 |       1852 |     1858 |         50 |       51 | 37.45 |
    |        5205 |       1852 |     1859 |         50 |       51 |  38.3 |
    |        5205 |       1852 |     1860 |         50 |       51 |  38.6 |
    |        5205 |       1852 |     1861 |         50 |       51 |  41.2 |
    |        5205 |       1852 |     1862 |         50 |       51 | 41.85 |
    |        5205 |       1852 |     1863 |         50 |       51 | 46.15 |
    |        5205 |       1852 |     1864 |         50 |       51 |  48.2 |
    |        5205 |       1852 |     1852 |         51 |    99999 | 24.25 |
    |        5205 |       1852 |     1853 |         51 |    99999 | 29.85 |
    |        5205 |       1852 |     1854 |         51 |    99999 | 29.85 |
    |        5205 |       1852 |     1855 |         51 |    99999 |  31.3 |
    |        5205 |       1852 |     1856 |         51 |    99999 | 34.85 |
    |        5205 |       1852 |     1857 |         51 |    99999 |  36.6 |
    |        5205 |       1852 |     1858 |         51 |    99999 | 37.45 |
    |        5205 |       1852 |     1859 |         51 |    99999 |  38.3 |
    |        5205 |       1852 |     1860 |         51 |    99999 |  38.6 |
    |        5205 |       1852 |     1861 |         51 |    99999 |  41.2 |
    |        5205 |       1852 |     1862 |         51 |    99999 | 41.85 |
    |        5205 |       1852 |     1863 |         51 |    99999 | 46.15 |
    |        5205 |       1852 |     1864 |         51 |    99999 |  48.2 |

  [1]: http://sqlfiddle.com/#!3/57c6d/1
  [2]: http://sqlfiddle.com/#!3/57c6d/1/0
  [3]: http://sqlfiddle.com/#!3/57c6d/1/1

Open in new window

0
W.E.BAuthor Commented:
Thank you all guys.
Anoo S Pillai solution is right on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.