Friday, 6 October 2017

SQL SELECT INTO Statement ~ niit post

  • SELECT INTO copies data from one table into a new table.
  • SELECT INTO creates a new table located in the default filegroup.

The SQL SELECT INTO syntax


The general syntax is: 

1.  SELECT column-names
2.    INTO new-table-name
3.    FROM table-name
4.   WHERE EXISTS 
5.        (SELECT column-name
6.           FROM table-name
7.          WHERE condition)

The new table will have column names as specified in the query. 


SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax


SQL SELECT INTO Example



Problem: Copy all suppliers from USA to a new SupplierUSA table. 


1.  SELECT * INTO SupplierUSA
2.    FROM Supplier
3.   WHERE Country = 'USA'


Results: 4 rows affected 


Here are the records in the newly created table SupplierUSA: 

Id
CompanyName
ContactName
City
Country
Phone
Fax
2
New Orleans Cajun Delights
Shelley Burke
New Orleans
USA
(100) 555-4822
NULL
3
Grandma Kelly's Homestead
Regina Murphy
Ann Arbor
USA
(313) 555-5735
(313) 555-3349
16
Bigfoot Breweries
Cheryl Saylor
Bend
USA
(100) 555-4822
NULL
19
New England Seafood Cannery
Robb Merchant
Boston
USA
(617) 555-3267
(617) 555-3389

No comments:

Post a Comment