Use SELECT inside an UPDATE query
Asked Answered
M

6

15

How can I UPDATE a field of a table with the result of a SELECT query in Microsoft Access 2007.

Here's the Select Query:

SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS

WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))

GROUP BY FUNCTIONS.Func_ID;

And here's the Update Query:

UPDATE FUNCTIONS

 SET FUNCTIONS.Func_TaxRef = [Result of Select query]
Mortimer answered 16/5, 2009 at 7:30 Comment(5)
If you are trying to do something special 90% it's a hack. Just think if it makes sense. I'm hoping that your database is normalized. Take a look at <a href="#825958 question</a>.Eparchy
See the answer to the question "MySQL/SQL: Update with correlated subquery from the updated table itself", maybe it will help.Flocculant
Usually you can turn a SELECT query into an UPDATE query with the click of a button in the Access query designer - there is a "query type" button in the command bar, when you are not in SQL view. Try it out, it takes care of any syntax requirements.Bacciform
Your comment is sounds like a joke.Mortimer
Do you see a smiley behind it? I'm not in the habit of writing joke comments that span multiple lines.Bacciform
C
22

Well, it looks like Access can't do aggregates in UPDATE queries. But it can do aggregates in SELECT queries. So create a query with a definition like:

SELECT func_id, min(tax_code) as MinOfTax_Code
FROM Functions
INNER JOIN Tax 
ON (Functions.Func_Year = Tax.Tax_Year) 
AND (Functions.Func_Pure <= Tax.Tax_ToPrice) 
GROUP BY Func_Id

And save it as YourQuery. Now we have to work around another Access restriction. UPDATE queries can't operate on queries, but they can operate on multiple tables. So let's turn the query into a table with a Make Table query:

SELECT YourQuery.* 
INTO MinOfTax_Code
FROM YourQuery

This stores the content of the view in a table called MinOfTax_Code. Now you can do an UPDATE query:

UPDATE MinOfTax_Code 
INNER JOIN Functions ON MinOfTax_Code.func_id = Functions.Func_ID 
SET Functions.Func_TaxRef = [MinOfTax_Code].[MinOfTax_Code]

Doing SQL in Access is a bit of a stretch, I'd look into Sql Server Express Edition for your project!

Coquina answered 16/5, 2009 at 10:16 Comment(0)
S
7

I wrote about some of the limitations of correlated subqueries in Access/JET SQL a while back, and noted the syntax for joining multiple tables for SQL UPDATEs. Based on that info and some quick testing, I don't believe there's any way to do what you want with Access/JET in a single SQL UPDATE statement. If you could, the statement would read something like this:

UPDATE FUNCTIONS A
INNER JOIN (
  SELECT AA.Func_ID, Min(BB.Tax_Code) AS MinOfTax_Code
  FROM TAX BB, FUNCTIONS AA
  WHERE AA.Func_Pure<=BB.Tax_ToPrice AND AA.Func_Year= BB.Tax_Year
  GROUP BY AA.Func_ID
) B 
ON B.Func_ID = A.Func_ID
SET A.Func_TaxRef = B.MinOfTax_Code

Alternatively, Access/JET will sometimes let you get away with saving a subquery as a separate query and then joining it in the UPDATE statement in a more traditional way. So, for instance, if we saved the SELECT subquery above as a separate query named FUNCTIONS_TAX, then the UPDATE statement would be:

UPDATE FUNCTIONS
INNER JOIN FUNCTIONS_TAX
ON FUNCTIONS.Func_ID = FUNCTIONS_TAX.Func_ID
SET FUNCTIONS.Func_TaxRef = FUNCTIONS_TAX.MinOfTax_Code

However, this still doesn't work.

I believe the only way you will make this work is to move the selection and aggregation of the minimum Tax_Code value out-of-band. You could do this with a VBA function, or more easily using the Access DLookup function. Save the GROUP BY subquery above to a separate query named FUNCTIONS_TAX and rewrite the UPDATE statement as:

UPDATE FUNCTIONS
SET Func_TaxRef = DLookup(
  "MinOfTax_Code", 
  "FUNCTIONS_TAX", 
  "Func_ID = '" & Func_ID & "'"
)

Note that the DLookup function prevents this query from being used outside of Access, for instance via JET OLEDB. Also, the performance of this approach can be pretty terrible depending on how many rows you're targeting, as the subquery is being executed for each FUNCTIONS row (because, of course, it is no longer correlated, which is the whole point in order for it to work).

Good luck!

Smaze answered 16/5, 2009 at 9:46 Comment(3)
+1 Nice, DLookup(), even tough it will be slow for big tables. Btw it seems Access UPDATE does not allow a join on a query, but it does allow a join on a table (see my post.) Access = WeirdCoquina
@ewbi: you may be interested in this KB article, Update Query Based on Totals Query Fails (support.microsoft.com/kb/116142).Impeller
Access has also Domain Aggregate Functions like DAvg, DMin, DMax, DCount and more that can be helpful in this kind situation.Rudie
P
1

I had a similar problem. I wanted to find a string in one column and put that value in another column in the same table. The select statement below finds the text inside the parens.

When I created the query in Access I selected all fields. On the SQL view for that query, I replaced the mytable.myfield for the field I wanted to have the value from inside the parens with

SELECT Left(Right(OtherField,Len(OtherField)-InStr((OtherField),"(")), 
            Len(Right(OtherField,Len(OtherField)-InStr((OtherField),"(")))-1) 

I ran a make table query. The make table query has all the fields with the above substitution and ends with INTO NameofNewTable FROM mytable

Pekin answered 29/7, 2011 at 21:8 Comment(0)
C
0

Does this work? Untested but should get the point across.

UPDATE FUNCTIONS
SET Func_TaxRef = 
(
  SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
  FROM TAX, FUNCTIONS F1
  WHERE F1.Func_Pure <= [Tax_ToPrice]
    AND F1.Func_Year=[Tax_Year]
    AND F1.Func_ID = FUNCTIONS.Func_ID
  GROUP BY F1.Func_ID;
)

Basically for each row in FUNCTIONS, the subquery determines the minimum current tax code and sets FUNCTIONS.Func_TaxRef to that value. This is assuming that FUNCTIONS.Func_ID is a Primary or Unique key.

Campanula answered 16/5, 2009 at 7:50 Comment(6)
Access saysL: "Operations must use an Updateable query."Mortimer
@Mortimer Added TAKE 2. See if that makes a difference.Campanula
A google search says that "Operations must use an Updateable query." could be related to "write permissions on the database and/or folder containing the database." Are you sure the database is not read-only? Check the access file to verify it is not readonly? I'll remove TAKE 2 because of the syntax error.Campanula
Does the following work? Basically have to narrow it down to see where it is breaking. UPDATE FUNCTIONS SET Func_TaxRef = ( SELECT 1 )Campanula
No, it's what I myself tried first and get no result. Here's the Database Scheme i44.tinypic.com/35l6hoj.jpgMortimer
The ACE/Jet engine simply does not support this syntax. It has its own, proprietary UPDATE..JOIN syntax. The error "Operations must use an Updateable query" is kind of generic, meaning, "I can't do this."Impeller
G
0

I did want to add one more answer that utilizes a VBA function, but it does get the job done in one SQL statement. Though, it can be slow.

UPDATE FUNCTIONS
SET FUNCTIONS.Func_TaxRef = DLookUp("MinOfTax_Code", "SELECT
FUNCTIONS.Func_ID,Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS
WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))
GROUP BY FUNCTIONS.Func_ID;", "FUNCTIONS.Func_ID=" & Func_ID)
Guesswarp answered 3/7, 2013 at 22:16 Comment(0)
B
0

I know this topic is old, but I thought I could add something to it.

I could not make an Update with Select query work using SQL in MS Access 2010. I used Tomalak's suggestion to make this work. I had a screenshot, but am apparently too much of a newb on this site to be able to post it.

I was able to do this using the Query Design tool, but even as I was looking at a confirmed successful update query, Access was not able to show me the SQL that made it happen. So I could not make this work with SQL code alone.

I created and saved my select query as a separate query. In the Query Design tool, I added the table I'm trying to update the the select query I had saved (I put the unique key in the select query so it had a link between them). Just as Tomalak had suggested, I changed the Query Type to Update. I then just had to choose the fields (and designate the table) I was trying to update. In the "Update To" fields, I typed in the name of the fields from the select query I had brought in.

This format was successful and updated the original table.

Balm answered 17/3, 2014 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.