Q

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?


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 last published in October 2002

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close