如何使用 SQL 将文字 table 合并到雪花中现有的 table
How to merge a literal table into an existing table in snowflake, using SQL
我正在尝试将数据合并到我数据库中的现有 table 中。我要合并的数据不在现有的 table 中,所以我想在我的 SQL 查询中将其作为 table 文字提供给我的数据库。下面是我目前得到的代码 - 它不断出错并显示一条消息:
SQL compilation error: error line 8 at position 39 invalid identifier 'VALS.COL2'
merge into existing_table
using
(select col1, col2, parse_json(col3) from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3))
on existing_table.sales_order_id = vals.col2
when matched then
update set
existing_table.timestamp_utc = vals.colOne, existing_table.JSON = vals.colThree
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (vals.colOne, vals.colTwo, vals.colThree);
注意我正在使用 Snowflake 应用程序保存数据 - Snowflake 应用程序允许 parse_json() 函数。
我认为 table 值的别名在子 select 中,然后需要一个别名。我还没有测试过这个,它的格式不适合作为评论,所以如果它不起作用,我会删除这个答案:
merge into existing_table
using
(select col1, col2, parse_json(col3) from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3)) src
on existing_table.sales_order_id = src.col2
when matched then
update set
existing_table.timestamp_utc = src.colOne, existing_table.JSON = src.colThree
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (src.colOne, src.colTwo, src.colThree);
除了在上一个答案中已经更正的缺少 table 别名(使用 src 作为别名)之外,还有一些 errors/typos:
- parse_json(col3) 需要别名,添加为 col3
- 列命名不一致,在源查询列中命名为 col1、col2、col3,但在连接条件中列称为 colOne、colTwo、colThree
这是运行时没有错误的更正查询
merge into existing_table
using
(select col1, col2, parse_json(col3) as col3 from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3)) src
on existing_table.sales_order_id = src.col2
when matched then
update set
existing_table.timestamp_utc = src.col1, existing_table.JSON = src.col3
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (src.col1, src.col2, src.col3);
我正在尝试将数据合并到我数据库中的现有 table 中。我要合并的数据不在现有的 table 中,所以我想在我的 SQL 查询中将其作为 table 文字提供给我的数据库。下面是我目前得到的代码 - 它不断出错并显示一条消息:
SQL compilation error: error line 8 at position 39 invalid identifier 'VALS.COL2'
merge into existing_table
using
(select col1, col2, parse_json(col3) from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3))
on existing_table.sales_order_id = vals.col2
when matched then
update set
existing_table.timestamp_utc = vals.colOne, existing_table.JSON = vals.colThree
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (vals.colOne, vals.colTwo, vals.colThree);
注意我正在使用 Snowflake 应用程序保存数据 - Snowflake 应用程序允许 parse_json() 函数。
我认为 table 值的别名在子 select 中,然后需要一个别名。我还没有测试过这个,它的格式不适合作为评论,所以如果它不起作用,我会删除这个答案:
merge into existing_table
using
(select col1, col2, parse_json(col3) from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3)) src
on existing_table.sales_order_id = src.col2
when matched then
update set
existing_table.timestamp_utc = src.colOne, existing_table.JSON = src.colThree
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (src.colOne, src.colTwo, src.colThree);
除了在上一个答案中已经更正的缺少 table 别名(使用 src 作为别名)之外,还有一些 errors/typos:
- parse_json(col3) 需要别名,添加为 col3
- 列命名不一致,在源查询列中命名为 col1、col2、col3,但在连接条件中列称为 colOne、colTwo、colThree
这是运行时没有错误的更正查询
merge into existing_table
using
(select col1, col2, parse_json(col3) as col3 from
values
('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$),
('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
as vals(col1, col2, col3)) src
on existing_table.sales_order_id = src.col2
when matched then
update set
existing_table.timestamp_utc = src.col1, existing_table.JSON = src.col3
when not matched then
insert (timestamp_utc, sales_order_id, JSON)
values (src.col1, src.col2, src.col3);