Get a list of all used tables in a Postgresql SELECT query

删除回忆录丶 提交于 2019-12-07 16:59:35

问题


Is there a way to get all the tables used in complex SELECT query in Postgesql without using an actual SQL parser? ver. 9.5 and above will be used.


回答1:


Try:

create or replace function get_query_tables(p_query text) returns text[] language plpgsql as $$
declare
  x xml;
begin
  execute 'explain (format xml) ' || p_query into x;
  return xpath('//explain:Relation-Name/text()', x, array[array['explain', 'http://www.postgresql.org/2009/explain']])::text[];
end $$;

select get_query_tables('your query here');

dbfiddle




回答2:


TableList:=TStringList.Create; 
pgConnection1.GetTableNames(TableList,False);


来源:https://stackoverflow.com/questions/44811335/get-a-list-of-all-used-tables-in-a-postgresql-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!