leora
leora

Reputation: 196459

how can i convert one row of a database to two

i have a table with the column id, itemID and locationId.

i have now split one locationId into multiple ones so i want to:

Update all records where locationId is 10 and have two rows where locationId is set to 11 and 12 (ItemId would be the same for both rows)

So to start i have:

Id ItemID LocationID
1 1 10

and i want to end up with

Id ItemID LocationID
1 1 11
1 1 12

is there any convenient way of doing this given its an update and an insert at once

Upvotes: 3

Views: 59

Answers (1)

paxdiablo
paxdiablo

Reputation: 881213

Possibly there's a single statement that can do it but why wouldn't you just use transactions, a vendor-agnostic way:

begin transaction;
insert into TBL (id,itemid,locationid)
    select id, itemid, 11
    from TBL
    where locationid = 10;
update TBL
    set locationid = 12
    where locationid = 10;
commit transaction;

I always tend to use standard SQL where possible so as to be able to switch between vendors easily, never mind the fact I haven't switched vendors for over a decade now :-)

Still, it's nice to have the ability even if I never use it (though sometimes I'll go vendor-specific for performance reasons if required - I don't think that'll be necessary for this particular use case since it's probably not something you'll be doing a lot of).

Upvotes: 2

Related Questions