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.

CREATE TABLEを利用せずに一時テーブルを作成したい

Posted at

SQLServerには一時テーブルという機能があるのですが、既存のテーブルをもとにして一時テーブルを作成したい場合、INTOを利用します。

たとえば以下はテーブルXから一時テーブルYを作成するサンプルです。

SELECT * INTO #Y FROM X;

INTOを利用すると、何もないところからCREATE TABLEを利用せずに一時テーブルを作成することも可能です。

以下のSQLでは初期データを持った一時テーブルPを作成することができます。

SELECT *
INTO #P
FROM (VALUES
  (1, 'A'),
  (2, 'B'),
  (3, 'C')
) AS T (col1, col2);

環境情報

Microsoft SQL Server 2019 (RTM-CU9) (KB5000642) - 15.0.4102.2 (X64) 
    Jan 25 2021 20:16:12 
    Copyright (C) 2019 Microsoft Corporation
    Developer Edition (64-bit) on Linux (Ubuntu 18.04.5 LTS) <X64>
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?