Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "create table as (select * from)"

I want to use data from the existing table and create a new table using it. I am using local database (.mdf) file to store my data.

I am not able to execute the following line:

SqlCommand com = 
           new SqlCommand("create table newinfo as (select * from oldinfo)", connection);

Is there any alternative way to create a table in C# from the data selected from another table.

like image 620
vardos Avatar asked Aug 14 '15 08:08

vardos


2 Answers

You can use SELECT INTO:

SELECT * INTO YourNewTable FROM YourOldTable

See more here: https://technet.microsoft.com/en-us/magazine/dd401720.aspx

like image 109
scgough Avatar answered Oct 06 '22 00:10

scgough


To create a table from a select statement you use the following syntax

SELECT Field1, Field2, Field3
INTO NewTable
FROM ExistingTable
like image 37
Ben Robinson Avatar answered Oct 06 '22 00:10

Ben Robinson