The SQL SELECT INTO Statement
The SELECT INTO statement copies data from one table into a new table.
SELECT INTO Syntax
Copy all columns into a new table:
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
Copy only some columns into a new table:
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the old table. You can create new column names using the AS clause.
SQL SELECT INTO Examples
The following SQL statement creates a backup copy of Customers:
FROM Customers;
The following SQL statement uses the IN clause to copy the table into a new table in another database:
FROM Customers;
The following SQL statement copies only a few columns into a new table:
FROM Customers;
The following SQL statement copies only the German customers into a new table:
FROM Customers
WHERE Country = 'Germany';
The following SQL statement copies data from more than one table into a new table:
INTO CustomersOrderBackup2017
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Tip: SELECT INTO can also be used to create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:
FROM oldtable
WHERE 1 = 0;
'IT > SQLD' 카테고리의 다른 글
[SQL] 데이터 정렬(COLLATE) 변경 방법에 대해 알아보자. (0) | 2024.04.27 |
---|---|
[MSSQL] Lock 확인 / KILL 하기 (0) | 2024.04.14 |
[MSSQL] 11.LOCK(락)과 동시성(병행)제어 (0) | 2024.04.13 |
오라클 SqlPlus 원격으로 접속하는 방법 (0) | 2024.04.11 |
[Oracle] 오라클 SQLPLUS 원격 접속 방법 (0) | 2024.04.11 |