Here's the way I'd skin this cat...
I would amend my table definition (apologies, can't think of a better field name right now):
CREATE TABLE your_table (
Id int identity(1,1) NOT NULL
, SourceId varchar(50) NULL
, ActualVulnerabilityAlertId int NULL
, VulnerabilityAlertId As Coalesce(ActualVulnerabilityAlertId, Id)
);
Essentially I have changed VulnerabilityAlertId
to be a calculated field and added a new field to hold the "raw" data.
You can then populate the table like so:
INSERT INTO your_table (SourceId)
VALUES ('TEST 1');
INSERT INTO your_table (SourceId)
VALUES ('TEST 2');
INSERT INTO your_table (SourceId)
VALUES ('TEST 3');
INSERT INTO your_table (SourceId, ActualVulnerabilityAlertId)
VALUES ('TEST 4', 12345); -- Non-"default" value!
If you need to update a value then you need to refer to the ActualVulnerabilityAlertId
field:
UPDATE your_table
SET ActualVulnerabilityAlertId = 937
WHERE SourceId = 'TEST 2';
Results:
SELECT Id
, SourceId
, ActualVulnerabilityAlertId
, VulnerabilityAlertId
FROM your_table
ORDER
BY Id;
Id SourceId VulnerabilityAlertId ActualVulnerabilityAlertId
-- -------- -------------------- --------------------------
1 TEST 1 1 NULL
2 TEST 2 937 937
3 TEST 3 3 NULL
4 TEST 4 12345 12345