简体   繁体   中英

Update multiple records in MS Access table (UPDATE SELECT UNION ALL)

I want to update multiple records using a single update query in MS Access. I followed the idea mentioned in this answer ( https://stackoverflow.com/a/65027/2935885 ) about inserting multiple records using a single query.

when I try this query it works:

UPDATE my_table left join (select 'rowid' as col_1, 'updated_value' as col_2 from onerow) new_values ON my_table.col_1 = new_values.col_1 set my_table.col_2 = new_values.col_2

(onerow is a dummy empty table)

However, when I extend it to multiple new values rows, MS Access gives an error "Operation must use an updateable query".

UPDATE my_table left join (
select 'rowid1' as col_1, 'updated_value1' as col_2 from onerow 
union all 
select 'rowid2' as col_1, 'updated_value2' as col_2 from onerow 
union all 
select 'rowid3' as col_1, 'updated_value3' as col_3 from onerow 
) new_values ON my_table.col_1 = new_values.col_1 set my_table.col_2 = new_values.col_2

How to fix this? Or what's wrong in the query I made?

You can't use update queries with union queries, since the complete rowset needs to be updateable, and union queries are never updateable.

Consider using a sequence table instead of a dual (one row) table. A sequence table is a table filled with sequential numbers.

If you'd have a sequence table with 100 numbers, you'd be able to use something like this:

UPDATE my_table left join (
    select Choose(s.nr,'rowid1', 'rowid2', 'rowid3') as col_1, Choose(s.nr,'updated_value1','updated_value2','updated_value3')  as col_2 
    from Sequence s
    WHERE s.nr Between 1 And 3
) new_values ON my_table.col_1 = new_values.col_1 set my_table.col_2 = new_values.col_2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM