User7354632781
User7354632781

Reputation: 2274

How to create a table from the results of a SQL query?

How can I put the results of a query into a table which is NOT created?

For example, put the following query results into a new table EmployeeDetail which IS NOT CREATED. Create the table and put the results at the same time.

select a.Name, b.Id
from Database1 a left join 
     Database2 b
ON a.Id = b.Id

How can this be done?

Upvotes: 2

Views: 5857

Answers (6)

Lazer
Lazer

Reputation: 94970

For Oracle SQL*Plus, the following syntax is used

CREATE TABLE <table name> AS <your query>;

For example,

CREATE TABLE managers AS SELECT * FROM employees WHERE desg = 'MANAGER';

Upvotes: 1

Dustin Laine
Dustin Laine

Reputation: 38543

SELECT INTO

SELECT a.Name, b.id
INTO newTable
FROM from Database1 a 
LEFT JOIN Database2 b

Upvotes: 0

user400348
user400348

Reputation: 236

You can use:

CREATE TABLE table_name AS ...

where the ... is your query.

Here is a link with more documentation: http://developer.postgresql.org/pgdocs/postgres/sql-createtableas.html

Upvotes: 4

Beth
Beth

Reputation: 9617

Send the create table command before the sql to insert the rows.

Upvotes: -1

SQLMenace
SQLMenace

Reputation: 135161

You didn't specify your RDBMS system, but in SQL Server it would be like this

select a.Name, b.Id into EmployeeDetail 
from Database1 a left join Database2 b ON a.Id = b.Id

Upvotes: 3

Martin Smith
Martin Smith

Reputation: 453847

This will be RDBMS dependant. If you are using SQL Server you can use SELECT ... INTO

select a.Name, b.Id
into EmployeeDetail 
 from Database1 a left join Database2 b ON a.Id = b.Id

Upvotes: 2

Related Questions