简体   繁体   中英

Snowflake - referring to column groups by a custom name

Say I have

CREATE TABLE "test-table" (
  "field1" TEXT,
  "field2" TEXT,
  "field3" TEXT,
  ...
  "field60" TEXT)

and I want to insert 30 fields from another table, and 30 fields from another.

I would do this running

insert into "test-table"(field1,...,field30)
  select
    field1,field2,...,field30
  from "another-table"

In both situations I've used the same long list of fields, can I denote this by a custom object? Like fieldlist = [field1,field2,...,field30] and then run commands like insert into "test-table"(fieldlist) to tidy up the SQL?

Hi @Christopher Turnbull, Snowflake follows ANSI SQL 1999 and SQL 2003, and hence it has to be a standard approach to insert data in snowflake. You could achieve this using a stored procedure, however, snowflake does not have any such object which could get this done.

You can submit a feature request to snowflake as your requirement looks interesting.

You could refactor this code by creating a SQL UDF. For example, if you want to insert the number 7 in a table with many columns:

insert into many_cols
select * from table(to_many_cols(7));

The definition of that function would be something like:

create or replace function to_many_cols(b int) 
returns table(a string, b int, c string, d number, e float)
as $$
select null, b, null, null, null
$$;

Which would work on a table like:

create temp table many_cols (a string, b int, c string, d number, e float);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM