Subquery error message while running query - sql

UPDATE rm
SET rm.cost_sqft = ISNULL((SELECT
CASE
WHEN (rm.rm_cat IN ('8','NON-REPORT') AND rm.space_fee = 'No')
THEN '0'
ELSE rent_w.w * #rental_rate
END
FROM
#rental_weight AS rent_w, bl, rm
WHERE
rm.main_contact = rent_w.space_type
AND rm.description = rent_w.space_quality
AND bl.cost_type = rent_w.campuse_cost
AND bl.bl_id = rm.bl_id),'0')
WHERE rm.bl_id = #bl_id
AND rm.fl_id = #fl_id
AND rm.rm_id = #rm_id
While running this statement, I get this error:
Msg 512, Level 16, State 1, Procedure icat_rm_cost_sqft, Line 100 [Batch Start Line 2]
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

The error means that the subquery (the select) is returning more than one row for a given key to be applied to the update.
Consider the scenario where the update becomes something like this very contrived scenario:
Sample data in a table called Customer:
cust_id cust_type name
1 X Fred
1 Y jack
And the query which will generate the error:
update customer
set name = (
select "MR " || name
from customer
where cust_id = 1 /* returns Fred and Jack */
)
where cust_id = 1 and cust_type = 'X';
What name should customer 1X be updated to? "Mr Fred" or "Mr Jack"? the RDBMS can't decide for you, you need to modify the query as a correlated subquery (add a join condition on cust_type) or a subquery that just returns one row.
Granted the above is very contrived, but that is what the error is telling you.

Related

How to divide fields in Sub query and Sql Server Case Statement

I am getting the error below on execution of query:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
My query is as follows:
CASE
WHEN GL.LDCategory IN ('21069','21070','21051','21073') THEN
CASE
WHEN LDContractType = 'AMORT' THEN (SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE)
WHEN LDContractType = 'BULLET' THEN ISNULL(GL.LDSSAMOUNT,0)
END
END AS LDSSAMOUNT,
The query causing the problem: SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE. It must return one row of data only, yours is returning multiple.
Here there are two problems:
1- The one you stated in your question: Subquery can return more than 1 result. You need to set a "definer", to which data you want. For example: If you table is something like below:
ID Total_prft No_Of_Installment
1 15 3
2 20 5
Then you can do like: SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE WHERE ID = #ID, where #ID is a pre-defined variable (maybe you can get it from your GL table).
Or, as it is recommended by #Suraj Kumar, you can use TOP 1 keyword as well, if it works for you.
2- Another problem here is you are not checking if No_OF_Installment is null or zero, which will cause a division error. Please add a check by adding something like:
WHEN LDContractType = 'AMORT' THEN (
case when NO_OF_INSTALLMENT IS NOT NULL AND NO_OF_INSTALLMENT > 0
THEN SELECT TOTAL_PRFT / NO_OF_INSTALLMENT FROM InsightSource.BS.IS_H_LD_SCHEDULE
ELSE 0
END)
...

Subquery returning more then one value but it shouldn't

I'm trying to update a lt_esp table -- to say set the season_desc column to the description value from the tr_season column.
The season value in lt_esp table is equivalent to the id from tr_season. Each line in table has a season value, however the season value isn't unique it can repeat many lines can have the season = 224. What i want to happen is if season = 224 then we find the id 224 in tr_season an whatever the description there we update our lt_esp tables.
update a
set a.season_desc = b.[description]
from [tr_esp] a
join tr_season b on a.season = b.id
where customer_no = 3773541
-- a.season_desc is null
tr_season has id, description and a few other questions
tr_esp has season (equivalent to id) and season_desc (equivalent to description)
I keep getting this error message but it doesn't make sense because each line has only one season value.
Msg 512, Level 16, State 1, Procedure ltg_esp_trigger, Line 11
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.

updating one table to another sql

ok..i'm sure i'm having a brain fart moment..but for some reason i cant get this update to work. I have 2 tables, i'm pretty much trying to copy info from 8 columns to 8 columns in another table. ..
this is what i have so far
update a
set a.denialcharge_cost = b.denial_cost
, a.epd_cost = b.early_default_payment
, a.expeditecharge_cost = b.expeditecharge
, a.duediligence_cost = b.duediligence
, a.deskreview_cost = b.deskreview
, a.servicing_cost = b.servicingcharge
, a.mers_cost = b.merscharge
, a.qcplans_cost = b.qcplans
from orderheader a
inner join clients b
on a.prnt_id = b.id
i get the error
Msg 512, Level 16, State 1, Procedure UpdateOrderHeader, Line 13
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.
i have x number of clients in clients..and y number of orders in orderheader..every order can have 1 client..every client can have multiple orders...prnt_id in orderheader has the id in the clients table....any help would be appreciated... i'm trying to copy denial_cost, early_default_payment, expeditecharge, duediligence, deskreview, servicingcharge, merscharge, qcplans
from clients to orderheader
Based on this answer to a previous question, and all of the other troubleshooting that we did, it appears as though you have a trigger getting in the way somewhere. Try disabling it and running the update.
SOMETHING like that should work (with formatting)
update orderheader
set denialcharge_cost = b.denial_cost,
epd_cost = b.early_default_payment,
expeditecharge_cost = b.expeditecharge,
duediligence_cost = b.duediligence,
deskreview_cost = b.deskreview,
servicing_cost = b.servicingcharge,
mers_cost = b.merscharge,
qcplans_cost = b.qcplans
from clients b
where orderheader.prnt_id = clients.id
from what i understand of the error, you are trying do fetch only one result, somewhere and the subquery returns more than a field.
like
select (select * from bb) as count from cc gives an error
because the subquery returns more than a field from the nested query

Update the table with 2 column values

I want to update the table value comparing with two column values.
Query
UPDATE acc SET slloc =
(SELECT Location
FROM Duplication$
WHERE Duplication$.GROUP1 = acc.grpcd
AND acc.ccode = Duplication$.div)
The above query showing error as
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.
How can i change my query
Just rewrite your UPDATE to use a JOIN and you can update as many rows as you like...
UPDATE a SET slloc = d.Location
FROM acc a
JOIN Duplication$ d ON d.GROUP1 = a.grpcd
AND d.div = a.ccode
This error message means that there is more than one Location where:
Duplication$.GROUP1 = acc.grpcd AND acc.ccode = Duplication$.div
is true. You need to see when is that possible and if necessary rethink your strategy.

Wrong SQLServer syntax

this is what I want to achieve:
4 tables are involved:
Players with PlayerID as PK,
Competitions with CompetID as PK
Results with ResultID as PK and CompetID as FK
And the 4th table: PlayerResultts with ResultID + PlayerID as PK and CompetID as new column I created.
Competitions, results and PlayerResults are already populated and quite large (300000 PlayerResults so far).
In order to populate the PlayerResults.CompetID column, I try a Update ... (Select....) request but I'm not aware of the right syntax and it fails.
Here is my feeble attempt:
update PlayerResults
set competid = (select distinct(r.competid) from results r, playerresults p
where r.resultID = p.resultid)
Error is (of course):
"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."
Can someone put me in the right direction? TIA
You don't need distinct
update PlayerResults
set competid = r.competid
from results r
where r.resultID = PlayerResults.resultid