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
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
This was first published in October 2002