0

Sorry I'm pretty new on this i would like to run something like the code but without storing it, just output the data as i am a read only user in the database I want to run it. BTW i know that the code there is very simple. I just wanna know how to print the data as it is an example.

CREATE [OR REPLACE] FUNCTION a ()
RETURNS timestamp
LANGUAGE plpgsql   
AS $variable_name$     
DECLARE 
  time_a timestamp; 
BEGIN     
  SELECT facilitytime 
     INTO time_a 
  FROM metrics 
  ORDER BY facilitytime DESC 
  LIMIT 1;   

  RETURN time_a;
END;    
$$  
  • Use `select a()` to "print" the result –  Mar 23 '22 at 10:08
  • Sorry but that would store it, right? I would like to output the data directly – Reikryv Mar 23 '22 at 10:11
  • Then run the SELECT statement directly –  Mar 23 '22 at 10:16
  • There is were i have a problem as i want to use variables and multiple selects – Reikryv Mar 23 '22 at 10:19
  • Then describe _that_ problem, not some code you aren't actually using. –  Mar 23 '22 at 10:19
  • It is that code but with more selects a variables – Reikryv Mar 23 '22 at 10:22
  • I think what you are looking for is anonymous code blocks (available since PG 9). Look at the answers [here](https://stackoverflow.com/questions/766657/how-do-you-use-variables-in-a-simple-postgresql-script) for some examples – Jonathan Willcock Mar 23 '22 at 14:12
  • @JonathanWillcock,. Except `DO`(anonymous) functions can't return values, you either have to 1) `RAISE NOTICE` in the function or 2) Inside the function `INSERT/UPDATE` the values into a table, which the OP says they can't do. – Adrian Klaver Mar 23 '22 at 15:44
  • It seems me given the restrictions, that you need to create a function outside the database in the language of your choice. – Adrian Klaver Mar 23 '22 at 15:46
  • @AdrianKlaver Yes it was `RAISE NOTICE` that I had in mind. The link I gave contained at least one example – Jonathan Willcock Mar 23 '22 at 16:08

0 Answers0