简体   繁体   English

Snowflake - 通过自定义名称引用列组

[英]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.我想从另一个表中插入 30 个字段,从另一个表中插入 30 个字段。

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?在这两种情况下,我都使用了相同的长字段列表,我可以通过自定义 object 来表示吗? Like fieldlist = [field1,field2,...,field30] and then run commands like insert into "test-table"(fieldlist) to tidy up the SQL?fieldlist = [field1,field2,...,field30]然后运行insert into "test-table"(fieldlist)之类的命令来整理 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.嗨@Christopher Turnbull,Snowflake 遵循 ANSI SQL 1999 和 SQL 2003,因此它必须是在雪花中插入数据的标准方法。 You could achieve this using a stored procedure, however, snowflake does not have any such object which could get this done.您可以使用存储过程来实现此目的,但是,雪花没有任何可以完成此操作的 object。

You can submit a feature request to snowflake as your requirement looks interesting.您可以向雪花提交功能请求,因为您的需求看起来很有趣。

You could refactor this code by creating a SQL UDF.您可以通过创建 SQL UDF 来重构此代码。 For example, if you want to insert the number 7 in a table with many columns:例如,如果你想在一个有很多列的表中插入数字7

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

The definition of that function would be something like: function 的定义类似于:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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