|
Suppose that you worked hard to write the query:
SELECT EmpNo, FirstName, LastName
FROM Employees
WHERE city = 'milan'
|
And you got results that you need to save urgently
| EmpNo | FirstName | LastName |
| 7 | anna | poperplatz |
| 9 | leonardo | rota |
How to do this ? There are two possible ways. First is to define empty table, and
insert into it the result set using [INSERT INTO] command:
CREATE TABLE Emp_subset
(
  EmpNo INT,
  FirstName CHAR(20),
  LastName CHAR(30),
CONSTRAINT [pk_Key] PRIMARY KEY (EmpNo)
);
INSERT INTO Emp_subset (EmpNo, FirstName, LastName)
SELECT EmpNo, FirstName, LastName
FROM Employees
WHERE city = 'milan'
|
Second option, is to add INTO [name_of_the_new_table]
after the the list of selected columns and before the FROM
word. Newly created table will inherite datatypes from columns of the underlying tables
that was used in the query. In our example EmpNo, FirstName, LastName columns will
have same types as in Employees table
SELECT EmpNo, FirstName, LastName
  INTO Emp_subset
FROM Employees
WHERE city = 'milan'
|
|