How to generate column names of a table with comma seperated in Oracle

 If you want to generate a comma-separated list of column names from a table, you can query the data dictionary views to retrieve the column names and then use string aggregation techniques to concatenate them into a single string.

Here's an example using Oracle SQL:

SELECT LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY column_id) AS column_names FROM all_tab_columns WHERE table_name = 'YourTableName';


In this query:

  • LISTAGG(column_name, ',') aggregates the column names into a comma-separated list.
  • WITHIN GROUP (ORDER BY column_id) specifies the order in which the column names should be concatenated. You can change the ORDER BY clause to suit your requirements.
  • all_tab_columns or dba_tab_columns is a data dictionary view containing information about columns in all tables accessible to the current user.
  • table_name = 'YourTableName' filters the results to only include columns from the specified table. Replace 'YourTableName' with the name of your table.

This query will return a single row containing a comma-separated list of column names from the specified table.

Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post