Building a SQL Server Table from SELECT Query Results (SQL Server 2008)
This guide demonstrates how to generate a new table in SQL Server 2008 using the output of a SELECT query. The following syntax achieves this:
<code class="language-sql">SELECT * INTO new_table_name FROM table_name WHERE condition;</code>
For instance, to construct a table called customers_in_california
populated with customer data from the customers
table residing in California, execute this query:
<code class="language-sql">SELECT * INTO customers_in_california FROM customers WHERE state = 'CA';</code>
Important Considerations:
INTO
keyword must directly precede the new table's name.WHERE
clause is optional; omit it to include all rows from the source table.Common Issue Resolution:
The error "Incorrect syntax near the keyword 'AS'" usually arises from using outdated syntax. The AS
keyword, while used for aliasing subqueries in older SQL Server versions, isn't required when creating tables from SELECT query results.
The above is the detailed content of How to Create a SQL Server Table from a SELECT Query Result?. For more information, please follow other related articles on the PHP Chinese website!