Обсуждение: Compile query results into a table

Поиск
Список
Период
Сортировка

Compile query results into a table

От
Santosh Udupi
Дата:
Hi,

I need to create a function in PostgreSQL for the following :

- Query multiple tables based on a business logic (all result sets return the same type of data)

- Compile all result sets into one table and return that table

 Is it possible to accomplish this without using the temp tables in PostgreSQL?

 Below is how I currently do it in Microsoft SQL server.

 Sample function:

---------------------------------------------------                                                                                                                                                                            

create  FUNCTION test(@search_in nvarchar(500))

RETURNS @data_table TABLE

                 ( item_id int, item_type nvarchar(1), first_name nvarchar(100), last_name nvarchar(100))

 AS

 BEGIN

                -- from first table

                if charindex('search_in_authors', @search_in) > 0

                                insert into @data_table

                                                select item_id, 'a', first_name, last_name

                                                from authors

                                                where first_name = 'james'

                -- from second table

                if charindex('search_in_editors', @search_in) > 0

                                insert into @data_table

                                                select item_id, 'e', first_name, last_name

                                                from editors

                                                where first_name = 'james'

                 -- from third table

                if charindex('search_in_publishers', @search_in) > 0

                                insert into @data_table

                                                select item_id, 'p', first_name, last_name

                                                from publishes

                                                where first_name = 'james'

 

                -- there could be more like these based on the business logic...

                (...)

                -- finally return the records compiled in @data_table

                RETURN

END

-----------------------------------------------------

Sample calls to the function:

 // select * from dbo. test  ('search_in_authors')

// select * from dbo. test ('search_in_authors, search_in_editors')

// select * from dbo. test ('search_in_authors, search_in_editors,search_in_publishers ')

-----------------------------------------------------

 Are there any options in PostgreSQL to achieve this other than using a temp table ?

Thanks,
Santosh

Re: Compile query results into a table

От
"David G. Johnston"
Дата:
On Thu, Jan 16, 2020 at 4:22 PM Santosh Udupi <email@hitha.net> wrote:


Sample calls to the function:

 // select * from dbo. test  ('search_in_authors')

// select * from dbo. test ('search_in_authors, search_in_editors')

// select * from dbo. test ('search_in_authors, search_in_editors,search_in_publishers ')

-----------------------------------------------------

 Are there any options in PostgreSQL to achieve this other than using a temp table ?

Since the names of the tables are dynamic you would need to use pl/pgsql and its EXECUTE capability and build up a useful SQL command on-the-fly.  To avoid using temporary tables you would need to put all the separate queries into the same main query.  This can be done with "UNION" and/(or?) Common Table Expressions (CTE - the WITH clause).

David J.