HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Returning table name and schema name along with column values

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
valuescolumnwithschemareturningnameandalongtable

Problem

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.

Solution

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.

Code Snippets

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

Context

StackExchange Database Administrators Q#320844, answer score: 2

Revisions (0)

No revisions yet.