1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL ServerのUPDATE文でJOINするときに別名を使う

Posted at

はじめに

SQL ServerでUPDATE文を書くときに、他のテーブルと結合して更新対象のレコードを絞りたい時があると思う。よく使用するのだがJOINや別名の位置を毎回忘れてしまうので、備忘録として残しておく。

書き方

SQL Serverの場合は下記でOK。他のDBだとJOIN方法から違ったりするので要注意。

UPDATE b
SET
    b.[item] = 1
FROM 
    [dbo].[tableB] AS b
INNER JOIN [dbo].[tableA] AS a
ON b.[ID] = a.[ID]
AND a.[num] = 1;

終わりに

DBごとにSQLが微妙に変わるのがめんどすぎる...

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?