operation must use an updateable query - access - ms-access

I want to update a table by using the join in access - 2007
UPDATE TABLE1 A INNER JOIN (SELECT ACCODE, SUM(AMOUNT) AS SUM_AMOUNT
FROM TABLE2 GROUP BY ACCODE) B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
it gives me error that
operation must use an updateable query
i have try with below query and no error is here
UPDATE TABLE1 A INNER JOIN TABLE2 B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
please help me to find what is wrong with first query

I think the reason Access treats your query as non-updateable is due to the subquery GROUP BY. You should be able to create an updateable query by using DSum.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE=" & a.ACCODE)
If ACCODE is text instead of numeric data type, add quotes around the value in the DSum expression.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE='" & a.ACCODE & "'")

I just had this issue and it was due to permissions on the table. I made a copy of the linked table to test my update query on, and kept getting this error message. I was pretty sure my query would be ok (it was a simple update) so I ran it on the actual table and I didn't get the error. Just wanted to add this for anyone else that comes across this in the future!

this error also comes when you are accessing database which is on different PC. so give write permission from security tab of folder and also give write access on sharing permission option.
In my case this is worked

Yes Mandeep, that is the way ms-access works.
UPDATE Table1 As T1 INNER JOIN Table2 AS T2 ON T1.F1 = T2.F1
SET T1.F2 = T2.F2
This query raises 'operation must use an updateable query' erro when T2 is a query, even when you are not updating T2.

Related

How to update a different database table with info from my current database table, using mySQL?

I'm trying to write a SQL query to update a field in a different database (Hosted on the same server with phpMyAdmin). I just want it to update the most recent row as the query will be run directly after a new user is added (on that new user). My goal output is to change the username field of this user to be the same as the email field of the user in the original database. The syntax error states
'syntax to use near 'FROM db2.users AS "data" WHERE db1.user.email =
...' at line 3'
I can't see where I'm going wrong - I saw another answer where a user said that mySQL does not support the FROM keyword, however I have not been able to find anywhere else that backs that up. This is what I currently have for my code:
UPDATE db1.user
SET username = data.username
FROM db2.users AS "data"
WHERE db1.user.email = data.email
AND db1.user.id = (
SELECT MAX(id)
FROM db1.user
)
LIMIT 1
If anyone knows where I'm going wrong with this it would be much appreciated!
In MySQL, the syntax would be:
UPDATE db1.user u JOIN
db2.users u2
ON u.email = u2.email JOIN
(SELECT MAX(u2.id) as max_id
FROM db1.user u2
) uu
ON uu.max_id = u.id
SET u.username = u2.username;
Notes:
It seems odd that you are not filtering by email to get the maximum id, but that is how your question is stated. Also, MySQL doesn't support LIMIT in multi-table UPDATE queries.
As in your SQL you are okay with using subqueries, I would suggest to try the following:
UPDATE
db1.user
SET
username = (
SELECT
data.email
FROM
db2.users AS "data"
WHERE
db1.user.email = data.email)
WHERE
db1.user.id = (
SELECT
MAX(id)
FROM
db1.user)
LIMIT 1;

SQLite / MySQL compatibility issue

I know SQL in SQLite is not completely implemented the same way as in MySql. My problem with the following queries is, that they are not compatible and I like to avoid a conditional if <DBMS> ... else
SQLite query
UPDATE sorties SET state = '#'
WHERE `key` IN (
SELECT `key` FROM sorties
INNER JOIN reports AS r
ON r.sortieId=sorties.`key`);
Error on MySQL:
SQL Error (1093): Table 'sorties' is specified twice, both as a target for 'UPDATE' and as a separate source for data
MySQL query (adapted from here)
UPDATE sorties AS s SET s.state='#'
WHERE s.`key` IN (
SELECT t.sortieId FROM (
SELECT r.sortieId AS sortieId
FROM reports AS r
INNER JOIN sorties AS sort
ON sort.`key`=r.sortieId)
AS t);
Error on SQLite:
SQLiteManager: Likely SQL syntax error: UPDATE sorties AS s SET s.state='#'
WHERE s.key IN ( SELECT t.sortieId FROM (
SELECT DISTINCT r.sortieId AS sortieId
FROM reports AS r
INNER JOIN sorties AS sort
ON sort.key=r.sortieId) AS t); [ near "AS": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
I can't figure out how to make this queries work on both systems equally!
All I want to have is, that each state of sorties must be '#' when it's key can be found in reports.sortieId.
Maybe there is a different approach for this?
Thank you
The first command reads the key value from the sorties table in the subquery, and then checks whether those key values exist in the sorties table in the outer statement. That check is superfluous; you can just compare the values to the ones in reports directly:
UPDATE sorties
SET state = '#'
WHERE key IN (SELECT sortieId
FROM reports);
As for the second command, SQLite does not support aliasing a table used in INSERT/UPDATE/DELETE because those commands work only on a single table. You can just remove the AS s and replace s with sorties everywhere.

Coldfusion ORM update with Join

I am looking for help on a HQL-statement, where I would like to do an update with a JOIN. I tried following statement, which runs fine in MySQL-Workbench:
UPDATE table1 t1
SET t1.status='Running'
JOIN t1.table2 t2
WHERE t1.status='Open' AND t2.destination ='mydestination' "
but it gives the error:
expecting "set", found 'JOIN' near line 1, column 16
It seems to me that as this is a bulk update operation then you should be using SQL for this not HQL. ORM is not really designed for this type of update. You can always run the SQL statement and then if you need to, load the object graph (your ORM entities) after.
As for the SQL you'll need to run it using cfquery (or the cfscript equivalent), from your HQL example it'd look something like this as SQL (assuming MySQL as you mention MySQL Workbench)
<cfquery>
UPDATE table1 t1
INNER JOIN table2 t2 ON t1.col = t2.col
SET status='Running'
WHERE status='Open'
AND t2.destination='mydestination'
</cfquery>
If you want to do it with HQL then you usually need to use a subquery. Something like this:
update table1 t1
set t1.status = 'Running'
where t1.state = 'Open'
and t1 in (
select t2.table1
from table2 t2
where t2.destination='mydestination'
)

how to correctly write an alias in a query SQL oracle

This is my query
CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid ,
(SELECT COUNT(*) as queue
FROM Waits t1
WHERE t2.code = t1.code AND t1.queue# <= t2.queue#)
FROM Waits t2;
I keep getting this compile error
Error at Command Line : 3 Column : 9
00998. 00000 - "must name this expression with a column alias"
although the Oracle SQL developer doesn't indicate any error beforehand. Also I believe that I am using the alias "query" so I really don't understand. Help would be appreciated
Try putting the alias after the subquery instead of after the column name within the subquery.
Although, I'd rewrite the query thus:
CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid , count(t1.code) queue
FROM Waits t1, Waits t2
WHERE t2.code = t1.code AND t1.queue# <= t2.queue# ;
I don't have access to Oracle now so I can't test this out. But give it a shot.
Upon reviewing your query, it's hard to tell exactly you're trying to do. Can you update the question with example data and an explanation of what you're trying to do?
I have come up with the solution. Previously I added the alias in the inner subqueries SELECT statement, however that wasn't the correct way. What I should have done is to make an alias of the ENTIRE subquery. So something like this...
CREATE VIEW CourseQueuePositions AS
SELECT t2.code, t2.cid,
(SELECT COUNT(*)
FROM Waits t1
WHERE t1.code = t2.code AND t1.queue# <= t2.queue#) AS queue
FROM Waits t2;

Self-referencing updates in HQL

I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.