# Save sql results in a variable and use it another sql

**URL:** <https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286>\
**Category:** Matillion ETL\
**Created:** [September 11, 2023, 3:19am UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286 "2023-09-11T03:19:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kanika3010](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@kanika3010](https://matillioncommunity.discourse.group/u/kanika3010)\
**Post date:** [September 11, 2023, 3:19am UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286/1 "2023-09-11T03:19:48Z")

</div>

I have a table from where I will be pulling dates and using those dates in where clause of another sql.

eg

query1

Test\_var= select date from date\_table

Query 2

select \* from table where date=test\_var

and append the data in a table

&nbsp;

Loop until the end of results of query 1

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![ASD](https://sea2.discourse-cdn.com/flex002/user_avatar/matillioncommunity.discourse.group/asd/32/1323_2.png) [@ASD](https://matillioncommunity.discourse.group/u/ASD)\
**Post date:** [September 11, 2023, 6:46am UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286/2 "2023-09-11T06:46:21Z")

</div>

Hi [@kanika3010](https://matillioncommunity.discourse.group/u/kanika3010)​

Store complete data from Query one in one table and use table iterator attached to transformation with Query two. Pass variable used in iterator through transformation scalar variable.

---

<div class="post-metadata">

**Author:** ![AnudeepK](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@AnudeepK](https://matillioncommunity.discourse.group/u/AnudeepK)\
**Post date:** [November 10, 2023, 12:08am UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286/3 "2023-11-10T00:08:50Z")

</div>

You can use a Query Result to Grid component to capture the results from the first query and use a Grid Iterator to iterate through the values passing to the transformation job. This way you can avoid creating another intermediate table.

---

<div class="post-metadata">

**Author:** ![kanika3010](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@kanika3010](https://matillioncommunity.discourse.group/u/kanika3010)\
**Post date:** [September 11, 2023, 10:46am UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286/4 "2023-09-11T10:46:20Z")

</div>

Can u create a small template job and post. I am really a basic user and dont know much about it

---

<div class="post-metadata">

**Author:** ![ASD](https://sea2.discourse-cdn.com/flex002/user_avatar/matillioncommunity.discourse.group/asd/32/1323_2.png) [@ASD](https://matillioncommunity.discourse.group/u/ASD)\
**Post date:** [September 11, 2023, 1:58pm UTC](https://matillioncommunity.discourse.group/t/save-sql-results-in-a-variable-and-use-it-another-sql/2286/5 "2023-09-11T13:58:08Z")

</div>

Hi [@kanika3010](https://matillioncommunity.discourse.group/u/kanika3010)​

I will try to attach job template but meanwhile steps here:

You will have to create one job variable in both transformation and orchestration.

1. 1st Transformation - which will run your Query 1 in SQL component and write it to a table using rewrite component.
2. 2nd Transformation - Second query in SQL component with {variable\_name} wherever required and write it to a table using rewrite component.
3. Orchestration - 

- Add 1st transformation
  - Add table iterator component pointing to table created above
  - The same variable will be used in table iterator 
  - Attach table iterator with 2nd Transformation
  - Add variable value to pass in 2nd transformation as scalar 

I hope this helps.
