Tech Kraft

Documentum, AWS, Java, Ruby on Rails, Linux, Windows, App Servers


SQL: How to insert new rows that are copies of existing rows

In SQL you can create/insert new rows that are essentially copies of the old rows, without needing to specify every column value.

Let’s say you have a table called “EVENTS” with the following columns:

  • event_name
  • location_name
  • promoter_name
  • event_date
  • promote_address
  • location_address

Now, let’s say you want to create a new row with all the same information except the event_name and event_date.

insert into events (event_name, location_name, promoter_name, event_date, promote_address, location_address) 
select 'new event' as event_name, location_name, promoter_name, 'todays date' as event_date, promote_address, location_address where promoter_name='Test1';

In the above sql, the select statement becomes the input for the insert statement. All the rows returned by the select statement will be inserted as new rows exactly as returned. Which means that instead of the ‘event_name’ as stored in the database, it will insert ‘new event’ as the event_name for each and every row. This is useful if you are trying to create new rows with the same “event_name” value.

 



Leave a comment

About Me

Senior Software Engineer professional with over 16 years of success with multiple open source technologies and various Content Management platforms and solutions.

Proven technical abilities through numerous projects involving enterprise web application design and development, application installation, configuration and support, and workflow and collaboration system designs.

  • Ability to learn new technologies and platforms quickly and apply them to the task at hand.
  • Excellent analytical skills, and strong communication and collaboration abilities.
  • Technical emphasis in including but not limited to Java, Ruby on Rails, Documentum and Alfresco
    in both Linux and Windows based environments

Newsletter