INSERT using values from another table

INSERT using values from another table

I have a simple question. I want to insert records to a table using the exceptions table results. For example:

insert into contact_arc 
     ( CONTACTID
     , CSUSERID
     , STARTDATETIME
     , CUSTOMERNUMBER
     )
values
     ( '00125200104261927300339'   -- rowid
     , 195
     , TO_DATE('2001-09-28 23:59:00'
             , 'SYYYY-MM-DD HH24:MI:SS'
             , 'NLS_CALENDAR=GREGORIAN')
     , 99999
     )

Now this is the thing: I want to select all records (6019 in total) and use only the rowid (that would be the contactid for the table I'm inserting into) added to the values section. In other words, I don't want to run 6019 times the insert statement changing manually the first value. Can anyone suggest how can this be done with a script or SQL statement?


    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

Instead of using the INSERT...VALUES syntax, use the INSERT...SELECT syntax.

insert into contact_arc 
     ( CONTACTID
     , CSUSERID
     , STARTDATETIME
     , CUSTOMERNUMBER
     )
select rowid
     , 195
     , TO_DATE('2001-09-28 23:59:00'
             , 'SYYYY-MM-DD HH24:MI:SS'
             , 'NLS_CALENDAR=GREGORIAN')
     , 99999
  from exceptions

The rowid column is selected from the exceptions table and is used to provide the values for the contactid column. Literals in the select statement, like 195, the date, and 99999, are used to provide the values for the other columns on every row inserted.

For More Information


This was first published in October 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.