Skip to main content
Question

how to implement cursor functionality in vertica

  • March 11, 2021
  • 4 replies
  • 13 views

luvpurohit

i want to loop through all the tables for a schema and do some operation on them like using a CTE to find the the count of duplicates in each table .

4 replies

luvpurohit
  • Author
  • New Participant
  • March 11, 2021

i have also used while loop and a temporary table in SQL Server,but loops are also not supported in vertica


marcothesane
Forum|alt.badge.img+1
  • Participating Frequently
  • March 11, 2021

You're faster when you avoid loops. Use SQL generating SQL. Like so, to count all rows in all tables in schema published:

SELECT 
  'SELECT '''
||table_schema||'''||''.''||'''||table_name||''' AS obj'
||',COUNT(*) AS rowcount FROM '
||table_schema||'.'||table_name||';'
FROM tables
WHERE table_schema='published';
                                                                ?column?
------------------------------------------------------------------------------------------------------------------------------------------
 SELECT 'published'||'.'||'MeasurementWithHistory_published' AS obj,COUNT(*) AS rowcount FROM published.MeasurementWithHistory_published;
 SELECT 'published'||'.'||'Installation_published' AS obj,COUNT(*) AS rowcount FROM published.Installation_published;
 SELECT 'published'||'.'||'ConsumerSession_published' AS obj,COUNT(*) AS rowcount FROM published.ConsumerSession_published;
 SELECT 'published'||'.'||'Item_published' AS obj,COUNT(*) AS rowcount FROM published.Item_published;
 SELECT 'published'||'.'||'item_Test' AS obj,COUNT(*) AS rowcount FROM published.item_Test;

The output you get, you can put into a file, and execute that file as a script.


BettyJones
  • New Participant
  • May 3, 2021

Using SQL will save time and resources; using loops will not give such a result. In urgent need, I use the service https://cashloansnearby.com/texas/lewisville/ to find a cash loan to pay for important services.


badrouali
Forum|alt.badge.img
  • Participating Frequently
  • May 3, 2021

In all cases, you can still use any programming language and connect to Vertica. You can then use "loops" if you want to.