Snowflake - TABLE <table_name> command - snowflake-cloud-data-platform

I am searching for official documentation of TABLE command(it is not the same as TABLE(<string_literal>)).
Snowflake supports TABLE <table_name>, which is a shorthand for SELECT * FROM <table_name>, but I am unable to locate its documentation. Example:
CREATE OR REPLACE TEMPORARY TABLE t AS SELECT 1 AS col;
SELECT * FROM t;
-- COL 1
TABLE t;
-- COL 1
db<>fiddle demo
It works with parametrized table name as well:
SET name = 't';
TABLE IDENTIFIER($name);
-- COL 1
TABLE TABLE($name);
-- error as TABLE() could be only used in FROM
I searched at All Commands/Query Syntax without much success.

I think the documentation you're looking for is Table Literals:
https://docs.snowflake.com/en/sql-reference/literals-table.html

I think you have it right in your comment on another answer. It's the PostgreSQL TABLE command, not a Snowflake feature.
TABLE Command
The command
TABLE name
is equivalent to
SELECT * FROM name
It can be used as a top-level command or as a space-saving syntax variant in parts of complex queries. Only the WITH, UNION, INTERSECT, EXCEPT, ORDER BY, LIMIT, OFFSET, FETCH and FOR locking clauses can be used with TABLE; the WHERE clause and any form of aggregation cannot be used.

Related

Column name Arguments in the "With" Statement in SQL Server

From the Microsoft online document (https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15), it mentioned that the "With" statement can have a column name as an argument, and then it says that:
"The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition."
What is "if distinct names for all resulting columns are supplied in the query definition" actually means? I use the "With" statement very often, but I never specify column names in the argument.
I tried to go through the entire document but it appears nowhere have explained this in further detail.
Does anyone know under what situation I need to put specify the column name?
Thanks in advance!
Quite simply, the resultset of the query that defines the CTE must return a set of columns with distinct names. For example, the following will not work:
with cte as (select 1 as x, 2 as x)
select * from cte;
The resultset has 2 columns named "x". In such a case, you MUST supply the column names in the definition of the cte since the query produces a resultset with duplicate names. So you would need to use the form:
with cte(x, y) as (select 1 as x, 2 as x)
select * from cte;
As a general matter, it is a best practice for any resultset to NOT have duplicate column names.

Can table columns be created by copying the datatype from another column ? (for ex like %type in Oracle)

For example, this is possible in Oracle. I wanted to know if snowflake has a similar concept.
CREATE TABLE Purchases
(
purchase_date calendar.date%type,
customer_nr customer.customer_nr%type,
purchase_amount numeric(10,2)
)
I'm afraid there's no way to do that right now. You can use system$typeof to check for a column type, but that can't be used in a create table statement.
The referenceability that you have in your example is not available. You can build a table by joining one or more tables and/or views together and build the column list with columns from any of the joins and any that you explicitly add to the list. The key is to join on 1 = 2 or FALSE
Example
CREATE OR REPLACE TEMP TABLE TMP_X
AS
SELECT A."name" AS NAME
,A."owner" AS OWNER
,B.STG_ARRAY
,NULL::NUMERIC(10,2) AS PURCHASE_AMOUNT
,NULL AS COMMENT
FROM TABLE_A A
JOIN TABLE_B B
ON 1 = 2
;
NAME - takes datatype from A."name" column
OWNER - takes datatype from A."owner" column
STG_ARRAY - takes datatype from B.STG_ARRAY column
PURCHASE_AMOUNT - takes the datatype explicitly specified NUMERIC(10,2)
COMMENT - no explicit datatype -- takes default datatype of VARCHAR(16777216)

Temporary tables in hana

it it possible to write script in hana that crate temporary table that is based
on existing table (with no need to define columns and columns types hard coded ):
create local temporary table #mytemp (id integer, name varchar(20));
create temporary table with the same columns definitions and contain the
same data ? if so ..i ill be glad to get some examples
i am searching the internet for 2 days and i couldn't find anything useful
thanks
Creating local temporary tables based on dynamic structure definition is not supported in SQLScript.
The question would be: for what do you want to use it?
Instead of a local temp. table you can use a table variable in most cases.
By querying sys.table_columns view, you can get the list and properties of source table and build a dynamic CREATE script then Execute to create the table.
You can find SQL codes for a sample case at Create Table Dynamically on HANA Database
For table columns read
select * from sys.table_columns where table_name = 'TABLENAME';
Seems to work in the hana version I have. I'm not sure how to find out what the version.
PROCEDURE "xxx.yyy.zzz::MY_TEST"(
OUT "OUT_COL" NVARCHAR(200)
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
AS
BEGIN
create LOCAL TEMPORARY TABLE #LOCALTEMPTABLE
as
(
SELECT distinct 'Cola' as out_col
FROM "SYNONYMS1"
);
select * from #LOCALTEMPTABLE ;
DROP TABLE #LOCALTEMPTABLE;
END
The newer HANA version (HANA 2 SPS 04 Patch 5 ( Build 4.4.17 )) supports your request:
create local temporary table #tempTableName' like "tableTypeName";
This should inherit the data types and all exact values from whatever query is in the parenthesis:
CREATE LOCAL COLUMN TEMPORARY TABLE #mytemp AS (
SELECT
"COLUMN1",
"COLUMN2",
"COLUMN3"
FROM MyTable
);
-- Now you can add the rest of your query here as such:
SELECT * FROM #mytemp
I suppose you can just write :
create column table #MyTempTable as ( select * from MySourceTable);
BR,

Derby Database Table Column Name Format Inconsistent in Query

When query a Derby database, I find out that for some tables I have to double quote the column name and use table name to qualify the column name, but for some other tables I don’t need to. What happens to these tables and how can I make all tables the same and can query them without the double quote and the table name qualifier? I am using NetBeans IDE’s Sql Command tool. Below are those different queries.
Set schema app;
Select * from table1 where table1.”state” = ‘CA’;
Select * from table2 where state = ‘CA’;
Putting a tablename or column name in quotes, sometimes referred to by the jargon-y term "delimited identifiers" does two things:
Allows you to use words that are otherwise reserved keywords (e.g., naming a column "WHERE" or "SELECT")
Instructs the database system to process the name using case sensitive rules, rather than case-insensitive rules
So if you originally created "table3" with a CREATE TABLE statement that specified "table3" in double quotes like this, then you will forever after have to refer to it with the name in double quotes.
select * from table3
will be automatically processed by the database as if it was
select * from TABLE3
while
select * from "table3"
will successfully match the table you created as create table "table3"
See: http://db.apache.org/derby/docs/10.9/ref/crefsqlj34834.html

When to use with clause in sql

Can Anybody tell me when to use with clause.
The WITH keyword is used to create a temporary named result set. These are called Common Table Expressions.
A very basic, self-explanatory example:
WITH Administrators (Name, Surname)
AS
(
SELECT Name, Surname FROM Users WHERE AccessRights = 'Admin'
)
SELECT * FROM Administrators
For further reading and more examples, I suggest starting out with the following MSDN article:
Common Table Expressions by John Papa
In SQL Server you sometimes need the WITH clause to force a query to use an Index. This is often a necessity in spatial queries that can reduce query time from 1 minute to a few seconds.
select * from MyTable with(index(MySpatialIndex)) where...

Resources