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.

SELECTし同じテーブルの値をUPDATEする方法【SQLServer】

Posted at

#同じテーブルの列の値をUPDATEする
SELECTした値を同じテーブルの異なる列へUPDATEする方法。
業務系システムなら入荷の入数と出荷の入数が同じ場合等に使用できる。
よく見かける方法は INNER JOIN を使用する方法。

UPDATE TBA a
INNER JOIN TBA b
ON  a.col1 = b.col1
SET a.col5 = b.col7

とか

UPDATE TBA
SET a.col5 = b.col7
FROM TBA a
INNER JOIN TBA b
ON  a.col1 = b.col1

あとはサブクエリを使用する方法もあるが、
単純に同じ値を更新するだけなら以下の方法でも可能。

UPDATE TBA
SET col5 = col7
FROM TBA
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?