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

Viewed 198

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.

The error I am getting when running this query is as follows:

SQL compilation error: Unsupported subquery type cannot be evaluated

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.

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

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.

1 Answers

The syntax of NOT IN is [value] NOT IN [subquery] and not the other way round.

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