Advertisement
asanchez75

postgresql/procedure

Dec 17th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. https://stackoverflow.com/questions/24618672/function-to-loop-through-and-select-data-from-multiple-tables
  2. http://okbob.blogspot.fr/2010/12/iteration-over-record-in-plpgsql.html
  3.  
  4. CREATE OR REPLACE FUNCTION public.internalid_formaltable_name_lookup()
  5. RETURNS TABLE(natural_id text, name text, natural_id_numeric text) AS
  6. $func$
  7. DECLARE
  8. formal_table text;
  9. BEGIN
  10. FOR formal_table IN
  11. SELECT quote_ident(table_name)
  12. FROM information_schema.tables
  13. WHERE table_schema = 'public'
  14. AND table_name LIKE 'formaltable%'
  15. LOOP
  16. RETURN QUERY EXECUTE
  17. 'SELECT t.natural_id, t.name, t.natural_id_numeric
  18. FROM internal_idlookup i
  19. JOIN public.' || formal_table || ' t USING (natural_id_numeric)
  20. WHERE i.internal_id = 7166571'; -- assuming internal_id is numeric
  21. END LOOP;
  22. END
  23. $func$ LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement