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

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close