stefanR
stefanR

Reputation: 89

Save query in table in SQL

I have a simple SQL query that look like this

select distinct v.col1, v.col2, v.col3
from table v
where v.col1 is not null

I would like to save the result set returned from this query into a completely new table called test.

How can I do that?

Thank you

Upvotes: 0

Views: 3248

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

The standard method to create a new table uses create table as:

create table new_table as
    select distinct v.col1, v.col2, v.col3
    from table v
    where v.col1 is not null;

Not all databases support this construct; for instance, SQL Server uses into:

select distinct v.col1, v.col2, v.col3
into new_table
from table v
where v.col1 is not null;

You may find that a view is sufficient, but a view is different from a table -- it is executed each time it is referenced.

Upvotes: 0

DonKnacki
DonKnacki

Reputation: 427

you can create a view like that :

CREATE VIEW [View_name] AS
select distinct v.col1, v.col2, v.col3
from table v
where v.col1 is not null

more info about view : https://www.w3schools.com/sql/sql_view.asp

Upvotes: 1

Related Questions