CodeDezk
CodeDezk

Reputation: 1260

SQL duplicate a row and then update

I need to create the duplicate of a row, and then update it.

  NAME      LOCATION    FLAG
  ====      ========    ====
  name1     location1    0
  name2     location2    0

first I have to select the row with NAME = name1 and LOCATION=location1 and create duplicate row and then update the NAME column with new value.

I can select the row by

select NAME,LOCATION,FLAG from TABLE where NAME=name1 and LOCATION=location1

So the result should be like

  NAME      LOCATION    FLAG
  ====      ========    ====
  name1     location1    0
  name2     location2    0
  name3     location1    0

Upvotes: 1

Views: 79

Answers (1)

Philipp
Philipp

Reputation: 2796

You can directly insert your select and just select your new name as value for the name-column

INSERT INTO TABLE (Name, Location, Flag) 
    select "New Name",LOCATION,FLAG from TABLE where NAME=name1 and LOCATION=location1

Upvotes: 3

Related Questions