PostgreSQL Stored Procedure, How to Return Result Set -


i trying create stored procedure return result set. using dynamic query first created through string executed in end of procedure. don't know how return whole result of select statment.

my query works because tested (dont mind select query sample). how do properly? can me this?

here stored procedure:

  create or replace function gettranscriptdata(fromdate text, todate text, idno integer = 0)  returns  table(     id integer,     employee_id integer,     employee_name text,     client_id integer,     client_name text ) -- text -- $body$ declare whereclause text; fullsql text; records record; exequery text;  begin  if idno = 0     whereclause := 'where logs.timestamp  - interval ''12 hours''  >= ''' || fromdate || '''::timestamp ';     whereclause := whereclause|| ' , logs.timestamp  - interval ''12 hours'' <= ''' || todate || '''::timestamp'; else     whereclause := ' trans.trans_id in (1,2,3) '; end if;  --raise notice 'whereclause = "%"', whereclause;   fullsql:= 'select         trans.trans_id id, agent.account_id agent_id, agent.lastname || '', '' || agent.firstname agent_name,          client.account_id client_id, client.lastname || '', '' || client.firstname client_name       chat_transcript_archive trans       inner join client_session_archive csession on csession.client_session_id = trans.client_session_id       inner join client_queue_archive clientq on clientq.client_queue_id = trans.client_queue_id       inner join agent_session_archive asession on asession.agent_session_id = trans.agent_session_id       inner join agent_queue_archive agentq on agentq.agent_queue_id = trans.agent_queue_id       inner join accounts client on client.account_id = csession.client_id ' || whereclause || '        group trans.trans_id, agent.account_id, agent.lastname, agent.firstname, client.account_id, client.lastname, client.firstname        order 13';  raise notice 'full query = "%"', fullsql;  exequery := 'select * (' || fullsql || ') records'  return query execute exequery;  end $body$ language plpgsql; 

as found out, missed semicolon.

however making things complex yourself. removing whole line , doing:

return query fullsql; 

should trick more elegantly. nothing turning inline view give alias.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -