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!