# Echo Statement in the SP when no rows returned

**URL:** <https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392>\
**Category:** Procedural SQL\
**Created:** [January 11, 2023, 2:32am UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392 "2023-01-11T02:32:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![prathap\_26](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/p/94ad74/32.png) [@prathap\_26](https://www.singlestore.com/forum/u/prathap_26)\
**Post date:** [January 11, 2023, 2:32am UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/1 "2023-01-11T02:32:45Z")

</div>

Hi Hanson,  
Since there is problem with distributed joins (where shard key columns are different), how we are doing is extract the result set from driving table and then in the loop we are using the extracted result set and querying the other table in the loop.  
for i in (collect(first\_query))  
loop  
echo select \*  
from   
where t2.colname=(i)  
end loop;

we are returning blank when there is no corresponding match in the 2nd table. All I am trying to is stop echoing when there is no record retuned.  
With regards,  
Prathap Reddy G

---

<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:** [January 21, 2023, 3:58am UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/2 "2023-01-21T03:58:26Z")

</div>

I suppose you could run a count(\*) on a query inside the loop with the same from/where clauses, and if it is \> 0, then do the echo select. That’ll take more time but should work. To save effort, you could put the results in a temp table, then if the temp table has \> 0 rows, return them, then delete all rows from the temp table, and repeat.

---

<div class="post-metadata">

**Author:** ![prathap\_26](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/p/94ad74/32.png) [@prathap\_26](https://www.singlestore.com/forum/u/prathap_26)\
**Post date:** [January 30, 2023, 4:40am UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/3 "2023-01-30T04:40:04Z")

</div>

Hi Hanson, Thank you for your response, due to SLA requirements, we could not execute the same query twice one for checking record existence and then returning the results. Temp table in production environment is a big maintenance headache. The result set should be delivered to our UI thru api call. Any better approach is much appreciable.

Thanks  
Prathap Reddy G

---

<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:** [February 9, 2023, 7:46pm UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/4 "2023-02-09T19:46:05Z")

</div>

Another thought is, collect the inner query result in an array, then if it is empty, don’t do anything, else, somehow return the contents of the array.

I’m interested in the fundamental problem though. What was the performance issue with the original query that made you want to do this? Was it doing a broadcast or shuffle that was too expensive, and you are trying to do a more point-to-point lookup approach that was cheaper?

---

<div class="post-metadata">

**Author:** ![prathap\_26](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/p/94ad74/32.png) [@prathap\_26](https://www.singlestore.com/forum/u/prathap_26)\
**Post date:** [February 22, 2023, 12:37pm UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/5 "2023-02-22T12:37:07Z")

</div>

Hi Hanson, it’s doing broadcast and consuming lot of cluster resources because of the shard key on the tables is not same. Array approach won’t help up because we will have results returned from previous iterations.

---

<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:** [February 23, 2023, 1:19am UTC](https://www.singlestore.com/forum/t/echo-statement-in-the-sp-when-no-rows-returned/6392/6 "2023-02-23T01:19:50Z")

</div>

What I meant was, in the inner loop, do

query\_val = select \* from  
where t2.colname=(i);

array\_val = collect(query\_val);

if length(array\_val) \> 0 then  
echo contents of array\_val[0]  
end if

(Forgive my rough syntax – I just wanted to give a flavor of the approach I was thinking of.)
