简体   繁体   中英

Using variable in Snowflake COPY INTO command

I have a stored procedure which unloads data from table into a Azure blob container. The conditions of file unloading is the file should be unloaded into a date time partition folder.

2022-06-09/1651011730.067872/FileName.csv

I have written the following stored procedure:

execute immediate 
  $$
    declare
        date_partition varchar;
        date_time_partition varchar;
        
    begin
     
     set date_partition = '2022-06-09';
     set date_time_partition = '1651011730.067872';
     
     COPY INTO @STAGE/:datetime/:date_time_partition/filename 
        FROM (
            select * from table
                where ID = 1
          ) file_format = CSV_FORMAT HEADER = TRUE DETAILED_OUTPUT = TRUE overwrite = true;
    
    return date_partition;
    end;
    
  $$;

The problem I am facing is the data is getting on loaded in this format

date_partition/date_partition/FileName.csv

What can I do to use the variables in the COPY INTO command?

Something like works for me:

execute immediate
$$
declare
  date_partition varchar default '2022-06-09';
  date_time_partition varchar default '1651011730.067872';
  copy_into varchar default 'COPY INTO @~/' || date_partition || '/' || date_time_partition || '/filename FROM (select * from citibike_trips limit 10) file_format = MYCSV HEADER = TRUE DETAILED_OUTPUT = TRUE overwrite = true';
begin
  execute immediate :copy_into;
  return date_partition;
end;
$$;

When I list the stage I see:

2022-06-09/1651011730.067872/filename_0_0_0.csv.gz

Is this what you're looking for?

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