UPDATE query based on a saved SELECT query in Access? - ms-access

Using a UPDATE query, is it possible to reference to a stored select query?
I'd like to accomplish something like this:
UPDATE ... WHERE ... IN [MY_STORED_PROCEDURE]

Perhaps something on these lines:
UPDATE ...
WHERE ID IN (SELECT ID FROM MyStoredProc)
Depending on your set up, a join may also be possible. You can add stored queries to the query design window, but you do not always end up with an updatable query, it usually depends on your indexes.

Related

VBA Access - Update statement using In

I can't find out if the below is possible. I want to run an update statement in one database from another database. I know that I can save a query and run that from the master database but I thought the below would be possible
UPDATE tblOne
INNER JOIN tblTwo
ON tblOne.[TID] = tblTwo.TID
SET tblTwo.PC = [tblOne].[PC]
from tblOne in 'C:\DB.mdb' ;
Any help would be greatly appreciated
Per the MSDN Documentation, the IN clause is only compatible when used in conjunction with the SELECT statement (i.e. Select queries), INSERT INTO statement (i.e. Append queries), or SELECT INTO statement (i.e. Make Table queries) - it cannot be used as part of an UPDATE statement.
I would suggest that you import tblOne as a linked table in the database in which you intend to execute your UPDATE query - this will also result in improved performance over the use of the IN clause.
To link the table, select Access Database from the Import panel of the External Data ribbon tab, and then select the Link option from the wizard:
After the table has been linked, your UPDATE statement will become simply:
UPDATE
tblOne INNER JOIN tblTwo ON tblOne.TID = tblTwo.TID
SET
tblTwo.PC = tblOne.PC
I think you just need to reverse what you’ve written because the table you’re updating needs to be the main table.
Try:
UPDATE tblTwo
INNER JOIN tblOne ON tblOne.[TID] = tblTwo.TID
SET tblTwo.PC = [tblOne].[PC] ;
Please note without a where clause you will be updating all rows.

cakePHP subquery in FROM

I need help to understand if something like this is posible on cakePHP...
I have a MYSQL query that retrive the information on the way it's needed, and it looks something like this:
SELECT *
FROM
(SELECT id ...)
JOIN
(SELECT id ...)
GROUP BY ...
So what I need to know if there is anyway on cakePHP to resolve the first FROM subquery.
I could break the query in two but I want to know if it can be solved in cakePHP using one Model->find command.
Thanks.
It cannot because your first "table" (in the FROM clause) is based on the model of the find. That being said
You can take your FROM subquery and make a SQL view out of it. Then create a Cakephp based on that view.
If your first subquery does not have a GROUP BY, the entire query can probably can be re-written without the subquery.

How to determine the number of rows returned from MYSQL select

I want to add a progress bar to my program.
Is it possible to read the number of rows that a select command is going to return before the data is sent?
No. You would have to do something like this before
select count(*) from your_table where ...
and afterwards the real query
select col1, col2 from your_table where ...
The reason is that the DB engine does not know how many records it will return until it applies the query and searches the data.
There is no way to determine how long a statement will take to process. If you want to know how many datasets a statement will return you will need to execute the statement to know.
I think Only way you can do is fire two queries
one with
select count(*) .....
and the other one is with
select * from ....
Select count(*) from `table` where <condition>.
MySql wouldn't know what to count if you don't specify what you're looking for.

Mysql update statement taking too long

I have a Mysql update statement and it's running too long - 52 sec
update table_ea ea, table_a a
set ea.match_creator='S', a.match_state=N
where
ea.source_id=a.asset_id and
ea.source_name='S' and
ea.match_creator='S' and
ea.entity_id like 'S'
Question:
a) Can we do an explain on this update statement in Mysql as we do for Select statements ?
b) Any suggestions on how to minimize the update time..
See how the corresponding select statement is performing. You are probably missing an index.
You'll need to post the table information if you want us to check.
Try posting SHOW CREATE TABLE table_ea and SHOW CREATE TABLE table_a
EXPLAIN SELECT ea.match_creator, a.match_state
FROM table_ea ea, table_a a
WHERE ea.source_id=a.asset_id
AND ea.source_name='S'
AND ea.match_creator='S'
AND ea.entity_id like 'S'`
You should create indexes to the following fields of the tables in order to make it quicker (it accelerates the joins):
ea.source_id
a.asset_id
ea.source_name
ea.match_creator
ea.entity_id
I also recomend that you replace the like operator for entity_id with an equal operator, cause in this case it is the same.

How to make a variable LIMIT in a query, using MYSQL

I'm trying to build a query with a variable limit. As far as I know I cannot do something like select * from table limit my_variable;, but I've read on the internet about a workaround:
SET SQL_SELECT_LIMIT = variable;
I have to write this syntax before the query I want to apply the LIMIT in. This works fine if I write a SELECT query after that code line, but it does not work if I write the following code instead:
INSERT INTO table
SELECT * FROM table1;
It will insert every record of the table1 in table2, instead of inserting the quantity of records specified in the first code line I wrote in this post.
How can I accomplish this?
Using prepared statements to dynamically compose SQL queries.