By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

Return dynamically created table from function

P: 3
Hello,

I have created one function which create dynamically pivot table .Now I want to return that table from function. Generally we return table with fixed number of columns with data type, But now I don know no of columns.Table will create dynamically and want to return that table. What should I do?
Please suggest.
Apr 28 '16 #1
Share this Question
Share on Google+
2 Replies


P: 3
Hello,
I am working on postgresql database. Please help for solution.
Apr 28 '16 #2

P: 3
Here is function

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION get_records()
  2.   RETURNS setof text AS
  3. $BODY$
  4. DECLARE
  5. query TEXT;
  6. table_name character varying;
  7. result text;
  8. BEGIN
  9. create temp table testing(a integer,b integer);
  10. insert into testing values(1,2);
  11. insert into testing values(3,4);
  12. table_name = 'testing';
  13. create table test as select * from testing;
  14. Perform (get_records_test('test')::test).* ;
  15. drop table testing;
  16. drop table test123;
  17. END;
  18. $BODY$
  19.  
  20.  
  21.  
  22.  
  23. CREATE OR REPLACE FUNCTION get_records_test(table_name text)
  24.   RETURNS SETOF text AS
  25. $BODY$
  26. DECLARE
  27. query TEXT;
  28. BEGIN
  29. query = format('SELECT  %s::text FROM %s', table_name, table_name);
  30. raise notice 'Query(%)',query;
  31. RETURN QUERY EXECUTE query;
  32. END;
  33. $BODY$
Apr 28 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.