简体   繁体   English

雪花多表插入使用何时和子查询不在目标表中的值

[英]Snowflake multiple table inserts using when and subqueries for values not in the destination table

I need to insert into multiple tables in parallel where each table should add values that don't exist and skip other values.我需要并行插入多个表,其中每个表应该添加不存在的值并跳过其他值。 I am testing over one table for now using this query:我现在正在使用此查询测试一张表:

INSERT ALL 
WHEN
  (
(
    SELECT
      PKEY 
    FROM
      TEMP) NOT IN ID
  )
THEN
  INTO TEMP (PKEY, TITLE) 
  VALUES
    (
      ID, TITLE_NAME
    )
    SELECT
      $1 AS ID,
      $2 AS TITLE_NAME 
    FROM
      @azure_stg/mti_test.csv (FILE_FORMAT => 'csv');

Each table has its own values that should be added so the when clause will be completely different than the other.每个表都有自己的值,应该添加这些值,这样when子句将与另一个完全不同。

The error I am getting when running this query is as follows:运行此查询时遇到的错误如下:

SQL compilation error: Unsupported subquery type cannot be evaluated SQL 编译错误:无法评估不受支持的子查询类型

Syntax of temp table:临时表的语法:

create or replace temporary table temp (pkey number, title varchar(32));

The CSV file contains 1101 rows with simple randomly generated IDs and titles. CSV 文件包含 1101 行,带有简单的随机生成的 ID 和标题。

I tried to use:我尝试使用:

INSERT ALL 
WHEN 
  ID NOT IN ( SELECT PKEY FROM TEMP) 
THEN
  INTO TEMP (PKEY, TITLE) 
  VALUES
    (
      ID, TITLE_NAME
    )
    SELECT
      $1 AS ID,
      $2 AS TITLE_NAME 
    FROM @azure_stg/mti_test.csv (FILE_FORMAT=>'GENERIC_CSV_FORMAT');

And got the following error:并得到以下错误:

SQL execution internal error: Processing aborted due to error 300010 SQL 执行内部错误:处理因错误 300010 中止

I've also checked this link on using insert all over tables with different when conditions我还检查了这个链接,在不同的时间条件下使用插入所有表

UPDATE更新

I used this query:我使用了这个查询:

INSERT ALL 
  WHEN 
    (SELECT COUNT($1) FROM @AZURE_BLOB_ONA/mti_test.csv (FILE_FORMAT=>'GENERIC_CSV_FORMAT') WHERE $1 NOT IN (SELECT PKEY FROM TEMP))>0
  THEN
  INTO TEMP (PKEY, TITLE) 
  VALUES
    (
      ID, TITLE_NAME
    )
    SELECT
      $1 AS ID,
      $2 AS TITLE_NAME 
    FROM @azure_stg/mti_test.csv (FILE_FORMAT=>'GENERIC_CSV_FORMAT');

It worked but now I need to add another subquery into the main insert so exclude duplications.它起作用了,但现在我需要在主插入中添加另一个子查询,以便排除重复。

The syntax of NOT IN is [value] NOT IN [subquery] and not the other way round. NOT IN 的语法是 [value] NOT IN [subquery] 而不是相反。

https://docs.snowflake.com/en/sql-reference/functions/in.html https://docs.snowflake.com/en/sql-reference/functions/in.html

Please try请尝试

INSERT ALL 
WHEN 
  ID NOT IN ( SELECT PKEY FROM TEMP) 
THEN
  INTO TEMP (PKEY, TITLE) 
  VALUES
    (
      ID, TITLE_NAME
    )
    SELECT
      $1 AS ID,
      $2 AS TITLE_NAME 
    FROM
      @azure_stg/mti_test.csv (FILE_FORMAT => 'csv');

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

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