Skip to main content

Generate a list of columns to be used in Insert or for the IN clause.


How often do we have to write an insert with explicit column list:

May be because the source we are inserting from do not have all the columns.

Or the destination table has that dreaded long column that you have to exclude.

Keep below SQL handy (Oracle database 11g onwards only). To quickly get a comma seperated list of columns for a table.

SELECT  LISTAGG(column_name , ',') WITHIN GROUP (ORDER BY column_id )  column_list
FROM   all_Tab_columns 
where table_name = upper(:TABLE_NAME) 
      and owner = upper(:OWNER) GROUP BY table_name;

Comments

Popular posts from this blog

Introduction to Oracle Analytical functions , rank , denserank.

In this video we tried to demonstrate how oracle anaytical function works. How to do Top N query with oracle's rank and dense_rank function. Also explained is how outputs and functionality of rank and dense_rank differs , what should be used when. The demonstration video is from Linux Mint OS and I have used my own Query Light application. Hope you find it enlightening.   Here are some of the screen prints.  Use of Oracle Rank Analytical Function. Oracle Top N Query using rank analytical functions.

Oracle Analytical Functions : Tutorial Part 2 Covers sum avg lag lead.

In this tutorial video we have demonstrated how to use oracle analytical functions like lag , lead , sum and avg etc. Example 1. Use of oracle function sum to display running totals with the use of unbounded preceding. select sum(Salary) over (order by salary rows unbounded preceding) running_total , salary , e.* from employees e Identify gap in the contiguous sequences with the lag function. Ex. In Employees table we have contiguous sequence of employees but due to some anomaly we found that there is now gap in the sequences. Ex Employee Id 210 comes after 206 which is not contiguous. We can write a query as given below to identify such sequence gaps. Oracle Lag Function select * from ( select lag (e.employee_id , 1) over (order by e.employee_id) as prev_emp, e.* from employees e ) tmp where (tmp.employee_id - tmp.prev_emp) > 1