SSブログ

AccessでUPDATEの更新値でサブクエリを使えない DMaxを使えばエラーにはならない [Accessクエリ]

えーと本日はAccessでのサブクエリの話題である。

DSC_1228[1].jpg

先日、Accessで他のテーブルにある値で更新かけたいんだけど、と言った質問を受けた。
何も考えずに、サブクエリ使えば良いのでは?
と答えたのだが、サブクエリを使うと「更新可能なクエリであることが必要です」のエラーになるというではないか。

えーそうなの?と思いSQLポケットリファレンス を見てみると...

UPDATE命令のサブクエリを使って更新のところをみると...



の注意書きが...
自分で書いておいて忘れていた。

じゃあ、結合して参照すれば?
と手のひら返しで答えたのではあるが、AccessってUPDATEでFROM句書けたっけか?

AccessではUPDATEにFROM句は書けないが、INNER JOINできる。

UPDATE foo INNER JOIN bar ON foo.a = bar.a SET foo.a = bar.b
WHERE c = 1


みたいに書けば良いらしい。ふーん。SQLポケリには書いてないなぁ。しまった。

でも単純に結合したテーブルの列を参照して更新するわけにはいかないようで、この作戦も失敗。
どうも結合すると複数行のデータが出てきてしまい、サブクエリでMAXを使わないといけないらしい。

どうしたものかとネットを検索しているとDMaxなら使えるような記事を発見。

そうか、DMaxなら使えるのか。前に記事を書いたかも。



DSumやらDMaxは、定義域集合関数っていう関数でサブクエリみたいな集合関数である。Access独自の関数である。詳しくは上の関連記事を参照して欲しい。

以下のクエリはAccessではエラーになるが

UPDATE foo SET a = (SELECT MAX(b) FROM bar) WHERE c = 1


以下のクエリなら実行可能。

UPDATE foo SET a = DMAX("b", "bar") WHERE c = 1


結論としては

AccessでUPDATEでサブクエリは使えないが、定義域集合関数なら使えるっていうこと。

本日は以上


DSC_1228[1].jpg

サイト内を検索

nice!(0)  コメント(9) 
共通テーマ:携帯コンテンツ

nice! 0

コメント 9

Troy

I'm gone to sayy to my litttle brother, that he should
also pay a quick visіt this webhpage on regular baѕis to
take updаted from hottest gossip.
by Troy (2023-11-25 22:37) 

Melisa

It's actuɑally a nice and usеfuⅼ ρiece of info.
I am happy tһat you simply shared thіs useful ibfo wit us.
Please stay ᥙs upp to date like this. Thank үou for sharing.
by Melisa (2023-11-28 18:30) 

Geri

Right hеre is the perfect site for anyone
who would liҝe to understand this topic.
You understand a whοle lot its almost tough to argue
with you (not that I actually would want to…НaHɑ).
You definitely put a new sρin on a topic which has bee dіscսssed for may years.
Great stuff, јust wonderfuⅼ!
by Geri (2023-12-01 03:10) 

Bonita

Α person essentially lend a hand too make severely articles I mivht state.
Thiis iѕ the very first tije I frequented your web pɑge and up to
now? I amazed ᴡith the research you made to
mae this actual publish incredible. Μagnificnt tasқ!
by Bonita (2023-12-02 11:00) 

Prince

Hey Ι am so excited I found yoᥙr web site, I really found you by
acсident, while Ι ᴡas searching on Google for something else,Anyways I am
here now and would just lіke to sѕay kudoѕ fⲟr а marvelos post and a all round thrillin blog (I also love the
theme/design), I don't have time to reaԁ it alⅼ at the moment but I have bookmarked it and also dded
in your RSS feeds, so whhen I have time I will be back to read
more, Ⲣⅼease ddo keep up tһһe fantastic job.
by Prince (2023-12-03 03:37) 

Douglas

Grеat post.
by Douglas (2023-12-03 09:07) 

Thanh

This is the ρerfect website for everyone who really wants to understand this topic.

Ⲩoս realize a whоle lot its almost tough tto argue with you
(not that I personally will neeɗ to…HaHa). You definitely put a
new spoin on a sսbject that's been ԝritten about for years.
Wonderfսl stuff, just great!
by Thanh (2023-12-07 18:31) 

Mandy

constаntly i used to read smalⅼer articⅼes orr review whicһ
as well clear their motіve, and that is also happening with this piece of writing whhіch I am reading here.
by Mandy (2023-12-09 00:03) 

Willie

Your modе of describing the whole thing in this piece of writing isѕ genuinely pleasant, every one
be aЬlе to without difficultyy understand it, Thanks a lot.
by Willie (2023-12-12 19:12) 

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。



Copyright Atsushi Asai Google+朝井淳
[改訂第4版]SQLポケットリファレンス

[改訂第4版]SQLポケットリファレンス

  • 作者: 朝井 淳
  • 出版社/メーカー: 技術評論社
  • 発売日: 2017/02/18
  • メディア: 単行本(ソフトカバー)

イラストで理解 SQL はじめて入門

イラストで理解 SQL はじめて入門

  • 作者: 朝井 淳
  • 出版社/メーカー: 技術評論社
  • 発売日: 2019/05/16
  • メディア: 単行本(ソフトカバー)

[データベースの気持ちがわかる]SQLはじめの一歩 (WEB+DB PRESS plus)

[データベースの気持ちがわかる]SQLはじめの一歩 (WEB+DB PRESS plus)

  • 作者: 朝井 淳
  • 出版社/メーカー: 技術評論社
  • 発売日: 2015/03/03
  • メディア: 単行本(ソフトカバー)

Access クエリ 徹底活用ガイド ~仕事の現場で即使える

Access クエリ 徹底活用ガイド ~仕事の現場で即使える

  • 作者: 朝井 淳
  • 出版社/メーカー: 技術評論社
  • 発売日: 2018/05/25
  • メディア: 大型本