Ryan Bennett
Ryan Bennett

Reputation: 83

Snowflake scripting - loop cursor and insert record into table

I am storing a select query in a resultset. I then loop the result set with a cursor. In my for loop is there a way I can insert the current record in a table. I dont wan to use INSERT INTO table (column1) values ('test').

I want to do it like INSERT INTO table(A) select * from record in cursor.

Upvotes: 1

Views: 5521

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 176074

It is possible:

DECLARE
    c1 CURSOR FOR SELECT col1, col2 FROM tab_src;
     
    col1 INT;
    col2 INT;
BEGIN
  FOR record IN c1 DO
      col1 := record.col1;
      col2 := record.col2;
  
      INSERT INTO tab_target(A,B)
      SELECT :col1, :col2;
  END FOR;
             
  RETURN 'End';
END;

Upvotes: 4

Related Questions