Update the table with 2 column values - sql

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.

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 multiple values ... not permitted to do so

declare #week1S DATE = '07/01/2016';
declare #week1E DATE = '07/07/2016';
WITH A as (
SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
Update ArbitrageResponse
Set arb_count1 = (SELECT ARB_accomplished_CNT FROM A)
I'm trying to update the column in one table based on a count of responses in another table. I'm trying to group on dispgroups and since my import table is massive I'm trying to filter it down to a selected date range. I'm going to have to run this a lot so It would be cool to keep the variables but not neccessary.
I want my response table to be recording responses every week so I just need columns being added to this table which is why I did an update.
I am getting an error
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
I'm not sure if this has to do with my variables as they are being called in my subquery - I know that my subquery is returning the aggregated results of many different rows. Is there a better way to do this?
Try something like this:
set #countaccomplished = SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name WHERE DISPATCH_GROUP_NAME = SOMETHING
Update ArbitrageResponse
Set arb_count1 = #countaccomplished
OR
WITH A as (
SELECT COUNT(ARB_accomplished) as ARB_accomplished_CNT, dispatch_group_name
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
Update b
Set b.arb_count1 = a.ARB_accomplished_CNT
FROM ArbitrageResponse b where a.dispatch_group_name= b.dispatch_group_name
Without knowing the structure of your table, this is just a guess:
WITH A as (
SELECT dispatch_group_name, COUNT(ARB_accomplished) as ARB_accomplished_CNT
FROM arbimport
WHERE Requested_date between #week1S and #week1E AND arb_accomplished LIKE 'Y'
GROUP BY dispatch_group_name
)
UPDATE ar
SET arb_count1 = a.ARB_accomplished_CNT
FROM ArbitrageResponse ar JOIN
A
ON ar.dispatch_group_name = a.dispatch_group_name

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?