Creating and Dropping Transient Tables in Snowflake - sql

If I want to create and drop a temporary table in SQL Server, I would do something like this:
drop table if exists #part_1;
select
whatever_columns
into
#part_1
from
table;
Does Snowflake have an equivalent? If so, what is it? I tried doing the following, but it is not working:
drop table if exists transient table part_1;
select
whatever_columns
into
transient part_1
from
table

Instead of SELECT .. INTO you can CREATE TABLE AS SELECT:
drop table if exists part1;
create or replace transient table part1
as
select 1 x, 2 y;

Related

I am not able to create temporary table in Bigquery

got below error while trying to create temporary table in Bigquery .
create or replace temporary table mss.Business.test2 as
select * from mss.Business.registration
Query error: Temporary tables may not be qualified at [2:36]
As mentioned by #Jaytiger names are not included in the CREATE TEMP TABLE statement.
You can create temporary table to store the results of a query as follows:
CREATE OR REPLACE TEMP TABLE <table1> as (SELECT * FROM `<dataset>.<table2>`);
SELECT * from <table1>
You can follow this documentation for further examples on Temp tables.

#temp table equivalent in Postgres?

In Sql Server, we can create temp table in sql editor like :
select * into #temp_sales_data from sales_data; -- this will create a temp table and insert data.
Please let us know what is the equivalent option in Postgres.
You just create one using create temp table
create temp table temp_sales_data
as
select *
from sales_data;

how to create a temp table in Dremio

I would like to create a temporary table as below in dremio
Select ABC into #temp_table
any advise?
I don't think DREMIO explicitly supports temporary tables. However, it does have the ability to create tables on the fly from the results of the query using create table as (CTAS):
create table temp_table as
select ABC;

Create temporary table from view

I am using sqlite3.
Suppose I have a view view_intermediate. I would like to create a temporary table from this view. In other words, turn the result of the view into a temporary table.
How should the SQL statement look like to do this?
SQLite supports CREATE TABLE AS..SELECT, so assuming you want the data in the table:
CREATE TABLE myTable AS
SELECT *
FROM view_intermediate;
If you want a table to be created from the view, but don't want the data, you can add a false condition.
CREATE TABLE myTable AS
SELECT *
FROM view_intermediate
WHERE 1=2;

How to copy a table schema with sql based on existance of the table?

I want to duplicate a table schema, but only if the target table does not yet exist:
CREATE TABLE IF NOT EXISTS my_table_secondary as select * from my_table_primary
Problem: running this if the my_secondary_table exists results in:
ERROR: Syntaxerror at "as".
What could I do to make this sql statement work?
(postgres 9.4)
To duplicate the schema of a table, you need a completely different syntax.
create table if not exists my_table_secondary (
like my_table_primary including all
);
INCLUDING ALL is an abbreviated form of INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS.
It doesn't duplicate foreign key constraints. See CREATE TABLE. Search for "The LIKE clause".
Try this approach:
CREATE OR REPLACE VIEW my_view AS SELECT * FROM my_table_primary;
CREATE TABLE IF NOT EXISTS my_table_secondary LIKE my_view;
INSERT INTO my_table_secondary
SELECT * FROM my_view
WHERE NOT EXISTS ( SELECT * FROM my_table_secondary );
More on CREATE TABLE ... LIKE: http://www.postgresql.org/docs/9.1/static/sql-createtable.html