0

I'd like to audit specific user activity across our application's database. Most of the important tables have these metadata fields:

  • id
  • date_last_edited
  • last_edited_by

I've got a prototype working:

with "params" as (
    select
        'DeethJ' as user,
        '2022-09-01' as from_date,
        '2022-09-30' as to_date
)

select 'address' as "table", id, date_last_edited, last_edited_by
from address
join params
    on last_edited_by = params.user
    and date_last_edited between params.from_date and params.to_date

union

select 'person' as "table", id, date_last_edited, last_edited_by
from person
join params
    on last_edited_by = params.user
    and date_last_edited between params.from_date and params.to_date

union

select 'person_person_relationship' as "table", id, date_last_edited, last_edited_by
from person_person_relationship
join params
    on last_edited_by = params.user
    and date_last_edited between params.from_date and params.to_date

Is there a way I can provide a list of table names and do the same query on each one - some kind of union_agg function?

Constraints: the database is PostgreSQL 9.6 (yes, I know, they said they will update it), my access is the "Freehand SQL data source" within SAP BusinessObjects so I can only provide a single statement. I cannot create a function or procedure.

Prospects look dim so I might instead generate the query in Excel...

2
  • 1
    No, you have to spell it out. You can write client code to construct the UNION for you. Commented Oct 6, 2022 at 12:23
  • Generating the query from the same spreadsheet I maintain to annotate the schema worked surprisingly painlessly. Commented Oct 6, 2022 at 13:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.