4

I have table in SQL:

public.employee

name age
abc 20
xyz 30

I want a query which would return the column values along with schema name and table name.

Input:

select * from public.employee

Output:

name age schema_name table_name

The reason for doing this is, I want to have an aggregate info table which has count for each schema and table.

Andriy M
23.3k6 gold badges60 silver badges104 bronze badges
asked Dec 12, 2022 at 10:05

1 Answer 1

2

You can display the table name in the result using the system column tableoid that is available in every table.

select *, tableoid::regclass::text as table_name
from public.employee;

This will however only display the fully qualified name if the table's schema is not in the search_path. If the table's schema is in the search_path, only the table name will be shown.

answered Dec 12, 2022 at 10:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.