title: Running query dynamically inside snowflake sql stored procedure
body: <p>I need to run the query which is saved in a field in one table and insert the value of query output into another table.
  To simplify the ask I have created dummy tables and try to test the logic.</p>
  <p>Table 1</p>
  <pre><code>create table tsql(id string, q string);
  insert into tsql values (1,'Select current_date');
  </code></pre>
  <p>Table tsql has select query in field name q.</p>
  <p>Table 2</p>
  <pre><code>create table tinput(d date);
  </code></pre>
  <p>Table 2 will get updated from the value in table 1.</p>
  <p>Below are the stored procedure I am trying to write. I know this can be done in javascript stored proc but I need to write this in sql as we are following SQL for all other.</p>
  <p>Procedure so far.</p>
  <pre><code>create or replace procedure sqlreadwrite(id string)
  returns string 
  language sql
  as
  $$
  Declare
  select_statement String;
  
  begin 
  create or replace temp table tk 
  as select q from tsql where id = :id;
  
  --select_statement := 'insert into tinp values (execute immediate 'Select Q from tk')';
  execute immediate 'insert into tinp values (execute immediate 'Select Q from tk')';
  return 'Success';
  end;
  $$;
  </code></pre>
  <p>Till now it is failing.</p>