I need to check if a column value exists for a record, and if so insert a duplicate of that record, with one of the fields updated.
The fields for my IncCodes table are are chars:
IncomeCode,
Description,
Location,
CostCentre,
NewIncomeCode
I already have an SQL command which updates all of the IncomeCodes from the NewIncomeCode and clears the NewIncomeCode column where present:
UPDATE IncCodes
SET IncomeCode = NewIncomeCode
,NewIncomeCode = ''
WHERE NewIncomeCode <> ''
AND Location = Location1
However I need command which does the same thing except instead of updating the IncomeCode field, creates a duplicate record with the IncomeCode updated by the NewIncomeCode field. Something like this pseudo sql:
INSERT INTO IncCodes
VALUES (SELECT NewIncomeCode
,Description
,Location
,CostCentre
,NULL
FROM IncCodes
WHERE NewIncomeCode <> '')
Any advice much appreciated. I can see similar questions about insert based on criteria but nothing specifically what I need.
Thanks in advance.
INSERT IGNORE INTO IncCodes
(IncomeCode, Description, Location, CostCentre, NewIncomeCode)
SELECT
NewIncomeCode, Description, Location, CostCentre, ''
FROM IncCodes
WHERE NewIncomeCode <> '' AND Location = Location1
;