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'
)
Related
Can please someone tell me why this query is not working in MySQL....Or how to get it work?
UPDATE table1, table2 SET abc = 0 FROM table2
WHERE table2.xyz > 0 AND table1.id_x = table2.id_x
Thank You
You appear to want a JOIN in an UPDATE. The correct syntax in MySQL is:
UPDATE table1 JOIN
table2
ON table1.id_x = table2.id_x
SET abc = 0
WHERE table2.xyz > 0;
Your query does not work for basically two reasons:
MySQL does not support FROM in the UPDATE statement (although SQL Server and Postgres do).
You have used table1 without defining it.
After connecting to a MySQL database using DBI I enable trace like this
$dbh->trace('SQL|DBD');
Then after this my SQL queries are printed twice:
>count_params statement SELECT table1.ID ID, table1.LinkID LinkID, Link.ID Link_ID FROM `table1` `table1` JOIN `table2` `Link` ON `table1`.`LinkID` = `Link`.`ID` WHERE table1.ID = ?
>parse_params statement SELECT table1.ID ID, table1.LinkID LinkID, Link.ID Link_ID FROM `table1` `table1` JOIN `table2` `Link` ON `table1`.`LinkID` = `Link`.`ID` WHERE table1.ID = ?
How can I print every executed SQL statement only once?
How can I print every executed SQL statement only once?
You can't. count_params and parse_params are internal functions used by DBD::mysql to emulate prepared statements on the client. Both are called when you execute a prepared statement.
count_params is called when a statement is prepared and contains the following:
if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), ">count_params statement %s\n", statement);
parse_params is called when a statement is executed and contains the following:
if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), ">parse_params statement %s\n", statement);
The logic is the same for both, so it's either all or nothing.
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;
I have written this query
set sql_safe_updates=0;
delete from t1 where id < (select avg(id) from t1);
set sql_safe_updates=1;
but it giving an error
Error Code: 1093. You can't specify target table 't1' for update in FROM clause
please resolve.
You can't specify the same table used in the delete in the rest of the query. One way to solve this is using a join:
delete t
from t1 t join
(select avg(id) as avgid
from t1
)
on id < (select avg(id);
This is a limitation in MySQL. Another way to get around it is to use an additional level of subqueries:
delete from t1 where id < (select avgid from (select avg(id) as avgid from t1));
The reason this works is because MySQL will materialize the subquery. The compiler sort of "forgets" that it is querying from the table bing modified, because of the extra level of subqueries.
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.