Ask the Expert

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

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

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: