Home >Database >Mysql Tutorial >How to Correctly Create a Table from a SELECT Query Result in SQL Server 2008?

How to Correctly Create a Table from a SELECT Query Result in SQL Server 2008?

Barbara Streisand
Barbara StreisandOriginal
2025-01-17 21:37:14621browse

How to Correctly Create a Table from a SELECT Query Result in SQL Server 2008?

SQL Server 2008: Building Tables from SELECT Query Results

Creating a table directly from a SELECT query's output in SQL Server 2008 can sometimes present challenges. The common approach using "CREATE TABLE temp AS SELECT..." often leads to syntax errors.

The correct method involves using the SELECT INTO statement:

<code class="language-sql">SELECT * INTO new_table FROM old_table;</code>

This command efficiently generates a new table ("new_table") mirroring the structure and data of an existing table ("old_table"). The asterisk (*) acts as a wildcard, selecting all columns.

For instance, to create a "CustomerContacts" table based on specific columns from a "Customers" table:

<code class="language-sql">SELECT customerID, name, contactNumber INTO CustomerContacts FROM Customers;</code>

This creates "CustomerContacts," populating it with the specified customer data.

The above is the detailed content of How to Correctly Create a Table from a SELECT Query Result in SQL Server 2008?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn