Write an INSERT query using another query

 

I think that title of this post isn’t matching exactly with what I want to write in it. The problem is like –

 

You want to write about 100+ INSERT/UPDATE statements for single column (say COL7) of a TABLE (say TEMP) because that column has 100+ different values in it.

 

There are three possible solution for doing this –

 

  1. You manually write 100+ statements for different column values of that table. It will be like –
    • INSERT INTO TEMP VALUES (‘A’,’B’,VAL1,’C’) WHERE …;
    • INSERT INTO TEMP VALUES (‘A’,’B’,VAL2,’C’) WHERE …; etc.
  2. You write single select statement for generating 100+ INSERT statements for the 100+ different column values which will be like –
    • SELECT ‘INSERT INTO TEMP VALUES(“A”, “B”, ‘||COL7||’, “C”) WHERE …;’
  3. Third way is of creating single procedure with single cursor with single FOR loop and it will solve the problem – the best way, I think.

 

I was just discussing it with a colleague and thought of posting it here.

 

Thank you, Boss. I still remember the things!!

1 thought on “Write an INSERT query using another query”

Leave a Reply

Your email address will not be published. Required fields are marked *