Wednesday, 15 February 2012

postgresql - Print ASCII-art formatted SETOF records from inside a PL/pgSQL function -


I would love to take advantage of the SQL output format of my PL / pgSQL function inside PostgreSQL I feel that I have to leave this idea and start to start.

I have my PL / PGSQL function query_result :

  create function query_releval (or_query text) ) Returns $$ as return SETOF record; Apply this code to Return Quest; End; $$ LANGUAGE plpgsql;   

.. Returns a SETOF record with an input text query, and which I can use with dynamic queries for my SQL scripting: Query_secult ('SELECT' ||: MYVAR || 'Alice') from ASC (ID int); mydb = # SELECT *; ID ---- 1 2 3

Then it was a inside to find a way to deliver this well formatted output from a PL / pgSQL Instead of the function does not support SETOF from ryes , and there is no magic predefined cast in it SETOF Text from record (I know I can make my own CAST ..)

If I create a dummy print_result function:

  create or Change function return the zero as print_result (this_query text) $$ query_result (this_query); End; $$ LANGUAGE plpgsql;   

.. I can not print formatted output:

  mydb = # SELECT print_result ('SELECT' ||: MYVAR || 'From Alice' ); Error: The set-value function is said in the context that can not accept any set ...   

Thanks for any suggestions (preferably working with PostgreSQL 8.4 is).

OK, set something in print_result with your result To do this, you have to loop on it, it will look something like this -

Here the result_record is defined as a record variable explanation For, we would also assume that you have a formatted_results variable defined as text and Rick to keep the results framed The string has been omitted in the string.

  for selection in result_record * as query_result (this_query) as the AS loop - with all this, you can do something like this in the formatted_results: = formatted_results | | ',' || Result_record.id; End loop; Return Formatted_Rates;   

So, if you change print_results to return the text, then declare the variable, as I have described and added it, your The function will return a comma separated list in all its results (with an extra comma at the end, I'm sure you can use PostGreysQL). I'm not sure that this is what you want but it should give you a good idea about manipulating your result set. You can get more information about control structures, which you should do more than you need.

Edit in response to the actual question:

The readable text is a feature of the psql client, as the ability to format Data Tulips is not a PostGrace SQL Server. To make this feature available in the server, the code or module will need to be removed from the psql utility and they must be compiled again as a database function. It is possible (and it is also possible that someone has already done this), but I do not know enough information with this process to provide a good description of how to do this. Most likely, the best solution for formatting the query results in text is that you can use PostgreSQL's string formatting function to implement the necessary features for your application.

No comments:

Post a Comment