1

For the following query:

UPDATE tempSpring_ASN AS t
SET t.RECORD_TYPE = (
        SELECT TOP 1 RECORD_TYPE
        FROM (
            SELECT "A" AS RECORD_TYPE
            FROM TABLE5
            UNION ALL
            SELECT "B" AS RECORD_TYPE
            FROM TABLE5
            )
        );

I'm getting, "Operation must use an updateable query." I don't understand. I'm not trying to update a union query. I'm just trying to update an otherwise updatable recordset with the output (single value) of a union query.

(The solution provided at Access SQL Update One Table In Join Based on Value in Same Table (which is also provided below) does not work for this situation, contrary to what is indicated on the top of this page.)

4
  • @invertedSpear , It's not a possible duplicate. I myself referenced it. I did this to make it easier for people to follow. :-)
    – as9876
    Feb 15, 2013 at 1:54
  • It's marked as such because it's not the intention of stack overflow to be a forum with back links to previous questions. If your question is different that's one thing, but if you are just supplying additional details or clarifications, as you appear to be doing here, you should make edits to the original question. The point is that a web search should turn up one question, not a question that requires following links to other questions in order to figure out what's going on. SO isn't just about helping you, it's supposed to easily help anyone in the future that has a similar problem. Feb 15, 2013 at 16:55
  • @invertedSpear , it sounds like you're claiming that I split it into 2 threads to make it easier for me, at the expense of everyone else using SO. Ironically, it's just the opposite! It would've been easier for me to have a single thread, but I specifically created a new one to make it easier for everyone else. In my particular situation, there was a single goal behind both questions, but in reality, the two questions can work independently of each other. JAGAnalyst's answer in fact, only answered the other post, not this one.
    – as9876
    Feb 18, 2013 at 0:35
  • @invertedSpear, therefore, would you please remove the "duplicate" tag, as this is inaccurate and will make it harder for others to use this site. Thanks!
    – as9876
    Feb 18, 2013 at 0:36

1 Answer 1

2

This question is a reference to a previous question, data and code examples posted here:

Access SQL Update One Table In Join Based on Value in Same Table

Hi AYS,

In Access, an Update query needs to be run on a table. As a UNION query is a combination of multiple sets of records, the result set is no longer a table, and cannot be the object of an Update query as the records in the result set are no longer uniquely identified with any one particular table (even if they theoretically could be). Access is hard-coded to treat every UNION query as read-only, which makes sense when there are multiple underlying tables. There are a number of other conditions (such as a sub-query in the SELECT statement) that also trigger this condition.

Think if it this way: if you were not using TOP 1 and your UNION query returned multiple results, how would JET know which result to apply to the unique record in your table? As such, JET treats all such cases the same.

Unfortunately, this is the case even when all of the data is being derived from the same table. In this case, it is likely that the JET optimizer is simply not smart enough to realize that this is the case and re-phrase the query in a manner that does not use UNION.

In this case, you can still get what you want by re-stating your query in such a way that everything references your base table. For example, you can use the following as a SELECT query to get the PO_NUM value of the previous SHP_CUSTOM_5 record:

SELECT
t1.SHP_CUSTOM_5
, t1.PO_NUM
, t1.SHP_CUSTOM_5 -1 AS PREV_RECORD

, (SELECT
t2.PO_NUM
FROM
tempSpring_ASN As t2
WHERE
t2.SHP_CUSTOM_5 = (t1.SHP_CUSTOM_5 -1)
) AS PREV_PO

FROM
tempSpring_ASN AS t1
;

You can then phrase this as an Update query as follows in order to perform the "LIN" updates:

UPDATE
tempSpring_ASN AS t1 

SET 
t1.RECORD_TYPE = "LIN"

WHERE
t1.PO_NUM=

(
SELECT 
t2.PO_NUM

FROM
tempSpring_ASN As t2

WHERE
t2.SHP_CUSTOM_5 = (t1.SHP_CUSTOM_5 -1)
)
;

This code was successful in the tests I ran with dummy data.

Regarding your "HDR" updates, your are really performing two separate updates. 1) If the PO_NUM matches the previous record's PO_NUM, set RECORD_TYPE to "LIN" 2) If it is the first record, set RECORD_TYPE to "HDR"

It is not clear to me why there would be a benefit to performing these actions within one query. I would recommend performing the HDR update using the "TOP 1" by SHP_CUSTOM_5 method you used in your original SELECT query example, as this will be a relatively simple UPDATE query. It is possible to use IIF() within an Update query, but I do not know what additional benefit you would gain from the additional time and complexity that would be required (it would most likely only be much less readable).

Best of luck!

1
  • There is nothing "wrong" with your query. Access just says you can't, so you can't.
    – JAGAnalyst
    Feb 15, 2013 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.