# Dynamic INSERT query in a Pipelined Procedure

**URL:** <https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144>\
**Category:** Uncategorized\
**Created:** [September 12, 2023, 3:50pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144 "2023-09-12T15:50:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlello](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@nlello](https://www.singlestore.com/forum/u/nlello)\
**Post date:** [September 12, 2023, 3:50pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/1 "2023-09-12T15:50:19Z")

</div>

Trying to figure this out without much success…

I’m trying to avoid creating nearly 100 pipelines; and build a pair of generic pipelines with procedures.

I am passing the data needed loading as JSON row images, and I’m successfully parsing the JSON to get keys (which equate to table column names) ; however I’m struggling to find a way to generate a dynamic INSERT statement.

EXECUTE IMMEDIATE does not ‘see’ the Query object passed into the procedure.  
TO\_QUERY / COLLECT aren’t suitable as they are designed for SELECT statements ?

Looking for ideas on how I can achieve this…

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [September 12, 2023, 9:25pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/2 "2023-09-12T21:25:39Z")

</div>

Consider putting the JSON data in a table for staging, then process it with a regular SELECT against that table, then clean it up from the staging table. The table could be a regular or temp or global temp table. If it’s a regular temp table, any work to process it would have to be in the same session.

---

<div class="post-metadata">

**Author:** ![nlello](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@nlello](https://www.singlestore.com/forum/u/nlello)\
**Post date:** [September 19, 2023, 6:21pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/3 "2023-09-19T18:21:51Z")

</div>

It was going so well when I tested the procedure… unfortunately:

Stored procedure containing ‘CREATE TEMPORARY TABLE tmp\_rowdata AS SELECT DISTINCT row\_data FROM batch WHERE row\_data IS NOT NULL’ cannot be run inside a pipeline.

---

<div class="post-metadata">

**Author:** ![nlello](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@nlello](https://www.singlestore.com/forum/u/nlello)\
**Post date:** [September 19, 2023, 6:22pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/4 "2023-09-19T18:22:05Z")

</div>

Thankyou Hanson, that worked well using a temporary table.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [September 19, 2023, 8:45pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/5 "2023-09-19T20:45:10Z")

</div>

What did you do to get by the error?

---

<div class="post-metadata">

**Author:** ![nlello](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/n/c57346/32.png) [@nlello](https://www.singlestore.com/forum/u/nlello)\
**Post date:** [September 21, 2023, 5:30pm UTC](https://www.singlestore.com/forum/t/dynamic-insert-query-in-a-pipelined-procedure/7144/6 "2023-09-21T17:30:11Z")

</div>

Hi Hanson,

I’ve created a Global Temp Table; I’m putting the query data (JSON) into that table then using EXECUTE IMMEDIATE to read from the GTT and insert into the target table.

This is actually really fast… I’m processing ~25k rows of data from a TSV into the target table via the GTT in less than a second.

This includes deleting rows from the target table that are flagged as deleted in the incoming query data.
