Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate multiple statements

Status
Not open for further replies.

jjlbmcnews

IS-IT--Management
Oct 4, 2001
60
0
0
GB
Hi,

I need to issue a number of insert statements and I know theres a way to generate all the statements dynamically so that I don't have to type each statement but I can't for the life of me remember how to do it. I know its something like select 'abc' = 'insert into table1 (field1, field2) values ('|| column from table2 ||', 'value2')
from table2 or something like that.
 
Do you want to generate a script that contains multiple insert statements, or write a single insert that creates multiple rows?

If you mean the latter, use something like this:

Code:
INSERT INTO your_target_table (
  column1,
  column2,
  etc )
SELECT tab.col1,
       tab.col2,
       tab.etc
FROM   your_source_table tab
WHERE  etc;

Otherwise, you have to generate a script that you subsequently run to create your data;

Code:
SET HEADING OFF
SET FEEEDBACK OFF
SET PAGES 0

SELECT 'INSERT INTO your_target_table (columns...) ' ||
        'VALUES ( ' ||
      '''' || char_or_date_column || ''',' ||
      numeric_column || ',' ||
      '''' || another_char_column || ''')'
FROM   your_source_table

SPOOL runit.sql
/
SPOOL OFF

@runit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top