Wrong SQLServer syntax - sql

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

Related

Subquery error message while running query

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.

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.

Update table with condition matching

Blockquote
I'm trying to mark with an 'X' all the records that match the fields Clave_Rel from table A with numero_cue from table B.
I'm doing something like this :
UPDATE clientes_MP_julio SET MARCA_X
= 'X' WHERE clientes_MP_julio.NUMERO_CUE = (SELECT CLAVE_REL FROM PAGOS A INNER JOIN clientes_MP_julio B ON A.CLAVE_REL =B.numero_cue)
But i'm receiving the message:
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.
I understand why the message, but I don't know how to solve it! Sorry... I'm new on this! and I really appreciate your help, hoping that someday I can help too.
Thanks!
You shouldn't need the subquery, just a simple inner join. The problem with your syntax is that the = sign expects a single value on the right side, whereas the subquery is returning multiple values.
Try the following:
UPDATE
clientes_MP_julio
SET
c.MARCA_X = 'X'
FROM
clientes_MP_julio AS c
INNER JOIN
PAGOS AS p
ON
p.CLAVE_REL = c.NUMERO_CUE

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

SSMS error: Subquery returned more than 1 value... but there is no subquery

The full error message is this one: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Everything I can find on it just says "yeah, just use TOP 1 and it'll be fine," only I don't even have a subquery or a select statement.
Here's the query:
update f
set f.new_monthnumber = datediff(mm, dateadd(dd, dbo.ufn_GetDaysInMonth(a.new_opendate) - 16, a.new_opendate), f.new_submit_date) + 1
from FilteredAccount a
left outer join FilteredNew_Financials f on f.new_name = a.accountnumber
where f.new_monthnumber is null
ufn_GetDaysInMonth is a user-defined function that analyzes the date passed to it and does not query the database at all, and in any case I've confirmed that it is not the cause of the error by running the query without it.
Anyone know what the heck is going on here?
Given that to write this query I would have swapped the tables and used an inner join, in order to have "FilteredNew_Financials" in both UPDATE and FROM clauses, pheraps there is a 1-to-n cardinality between the two tables on the join condition... maybe you have some dirty data in tables?
Are you sure "new_name" and "accountnumber" are primary and foreign keys (at least logically, if not structurally)?
I would have written the query slightly differenty:
update FilteredNew_Financials
set new_monthnumber = datediff(mm, dateadd(dd, dbo.ufn_GetDaysInMonth(a.new_opendate) - 16, a.new_opendate), f.new_submit_date) + 1
from FilteredAccount a
left outer join FilteredNew_Financials f on f.new_name = a.accountnumber
where f.new_monthnumber is null
Does that work?