UPDATE SELECT Access - ms-access

This is an MS Access 2010 related question.
Is it possible to go the short route (A) and write an UPDATE statement using a SELECT statement in order to catch the relevant value or do I have to go the long route (B) and I will firstly need to query the data through a SELECT statement that I will save as a query and then refer to this saved query in my UPDATE statement?
Here is (A):
UPDATE tbl_A
SET tbl_A.Header1 = (SELECT F1 FROM tblStaging
WHERE tblStaging.F1 = 'ISSUER CODE')
WHERE (((tbl_A.TableName)='tblStaging'));
Here is B:
SELECT F1
FROM tblStaging
WHERE F1 = 'ISSUER CODE';
UPDATE tbl_A, Q_A_Sel_ISSUERCODE
SET tbl_A.Header1 = [Q_A_Sel_ISSUERCODE].[F1]
WHERE (((tbl_A.TableName)='tblStaging'));
Thank you
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris gave the solution:
UPDATE tbl_A, tblStaging
SET tbl_A.Header1 = tblStaging.F1
WHERE (((tblStaging.F1)='ISSUER CODE') AND ((tbl_A.TableName)='tblStaging'));

I'm not sure about Access, but in SQL Server you can use a single UPDATE statement like this (and I would've thought it should also work in Access):
UPDATE A
SET Header1 = S.F1
FROM tbl_A A, tblStaging S
WHERE S.F1 = 'ISSUER CODE' AND A.TableName ='tblStaging';
Although if that's exactly what you want to do, it's the same as:
UPDATE tbl_A SET Header1 = 'ISSUER CODE'
WHERE TableName = 'tblStaging';

Related

Error Code 1242: Subquery retuens more than 1 row

I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?

Update values in one table based on values in another table where a specific criteria matches

I have picked up the query from another thread posted here and have edited based on the tables I have. I get some syntax error when I try to save it. What am I doing wrong here?
UPDATE cleanedGC
SET cleanedGC.Competency = DomainMapTBL.[Person SMU Name]
From cleanedGC, DomainMapTBL
WHERE cleanedGC.[Person SMU #]=DomainMapTBL.[Person SMU]
Correct syntax would be:
UPDATE cleanedGC
INNER JOIN DomainMapTBL
ON cleanedGC.[Person SMU #]=DomainMapTBL.[Person SMU]
SET cleanedGC.Competency = DomainMapTBL.[Person SMU Name]

Compare 2 columns, then perform action on another column in MySQL

I have a table named TableX in MySQL. There are 4 columns in TableX. The columns are ColumnCompare_Now, ColumnCompare_Past, ColumnNumber_Now, ColumnNumber_Past.
I want to write a MySQL statement that has the following logic;
If ColumnCompare_Now == 'ActionNeeded' and ColumnCompare_Past == 'ActionNeeded',
then ColumnNumber_Now = `ColumnNumber_Now` + `ColumnNumber_Past`
MySQL knowledge is limited to simple Select Where statements. How can the above operation be implemented in MySQL? Meanwhile, I will try to figure this out myself while waiting for help from fellow members of StackOverflow.
I'm not completely sure I know what you're trying to accomplish. If you want to select a new column with this information, you can use a case statement:
select
case when ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
then ColumnNumber_Now + ColumnNumber_Past
else ColumnNumber_Now
end as ColumnNumber_Now
from tablex
And if you need this in an update query:
update tablex
set ColumnNumber_Now = ColumnNumber_Now + ColumnNumber_Past
where ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
update TableX set ColumnCompare_Now=(select (ColumnNumber_NOw+ColumnNumber_Past) where ColumnCompare_Now=='ActionNeeded and ColumnCompare_Past=='ActionNeeded');
kindly try out this.

MySQL Update Value from SubQuery

I'm using MySQL and am running a query, which I think should work, but apparently I'm missing something.
0 records get updated when I run:
UPDATE `client`
SET StatementTermsID = (SELECT StatementTermsID FROM statementterms WHERE TermsDescription = 'NET 15')
WHERE `client`.StatementNote LIKE '%Net 15%';
If I run the subquery by itself, I get the record id as expected. If I change the subquery to be a static value, then 2000 plus records get updated. Any idea on what I'm missing?
Here's a different syntax:
UPDATE `client` a,
(SELECT StatementTermsID FROM statementterms WHERE TermsDescription = 'NET 15') b
SET a.StatementTermsID = b.StatementTermsID
WHERE a.StatementNote LIKE '%Net 15%';
i Think you should use a join. I can see that we cannot use StatementTermsID because thats what you are trying to update.So for sure it won`t be there in the former table.
If you have any ids to join Use that as well.
UPDATE client
SET StatementTermsID =statementterms.StatementTermsID
FROM client
INNER JOIN statementterms
WHERE statementterms.TermsDescription = 'NET 15' AND
client.StatementNote LIKE '%Net 15%';
Or else Try this syntax
UPDATE client , statementterms
SET client.StatementTermsID = statementterms .StatementTermsID
WHERE statementterms.TermsDescription = 'NET 15' AND
client.StatementNote LIKE '%Net 15%';

Update using Select Statement

I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"