How to include/reference a common bit of code in a function

From: "chandru(dot)aroor(at)yahoo(dot)com" <chandru(dot)aroor(at)yahoo(dot)com>
To: "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: How to include/reference a common bit of code in a function
Date: 2018-06-21 01:11:48
Message-ID: 2117788854.111664.1529543508328@mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi all,
I have a function that has multiple UNION ALLs. As part of the WHERE condition I need to test if any one of 31 columns is true. So right now I have my function looking something like:

Insert into Table A (column 1, ... column n) (SELECT (column 1, ...column n) from Table BWHERE ( column  A OR column B OR ...repeated 31 times) = trueAND Condition 1AND Condition 2AND Condition 3
UNION ALL
SELECT (column 1, ...column n) from Table CWHERE ( column  A OR column B OR ...repeated 31 times) = trueAND Condition 4AND Condition 5AND Condition 6
UNION ALL
SELECT (column 1, ...column n) from Table DWHERE ( column  A OR column B OR ...repeated 31 times) = trueAND Condition 7AND Condition 8AND Condition 9
UNION ALL repeated about 10 times.
)
My question is there anyway that I can substitute the list of 31 of OR columns (always the same 31 column names) in my WHERE clause  with a "shortcut" so my code is shorter and cleaner. I need to create additional functions for other tables that use the same 31 ORs, so trying to find an easy way to reference this list of ORs across all my functions. Note, the additional WHERE clauses differ for each of the Unions, so I have them numbered 1,2,3 for the first SELECT, 4,5,6 for the second SELECT etc. 
The above is a representation of the function and I have simplified it so I could highlight attention on the above question. 
Thanks! 
Chandru

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Laurenz Albe 2018-06-21 06:51:23 Re: How to include/reference a common bit of code in a function
Previous Message Damian Carey 2018-06-11 07:41:32 Re: Postgres on Unix/Debian - seeking "dummies guide"