How to automatically set the value of a not primary key column with the value of the primary key in an insert query?

StackOverflow https://stackoverflow.com/questions/21987896

Question

I am working on an INSERT query that insert a new row in a table named VulnerabilityAlertDocument

This table have 3 fields:

  • Id: that is an auto increment int and it is my PRIMARY KEY

  • VulnerabilityAlertId: this field is an int and must have the same value of **Id column

  • SourceId: is a varchar(50) and contains some text.

Now my problem is how to do that the VulnerabilityAlertId have the same value of the *auto increment Id value for this new record.

If I do something like:

INSERT INTO VulnerabilityAlertDocument 
       ( [VulnerabilityAlertId], [SourceId] ) 
VALUES 
       (4, 'TEST');

it create a new record in the table in whic the Id column value is not specify because it is auto increment, and the VulnerabilityAlertId value is 4

I need that in this query, the VulnerabilityAlertId value is automatically setted with the value of the Id value of the new row

What can I do to do it?

No correct solution

OTHER TIPS

As I understand your question is it like you want the VulnerabilityAlertId value automatically setted with the value of the Id column of the new row always? If yes then you should alter the table definition and add VulnerabilityAlertId column as a persisted computed column as:

alter table VulnerabilityAlertDocument drop column VulnerabilityAlertId;
alter table VulnerabilityAlertDocument add VulnerabilityAlertId as id persisted;

by doing this there's no need to specify the values for VulnerabilityAlertId column as it will be computed every time a new row is inserted.

Hope this helps!!

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

There are 2 ways..

  1. If you can split this into multiple statements, use @@Identity after inserting the record to update the recently inserted row.

or 2. make use of below built-in sql server function.

IDENT_CURRENT('table_name') + 1

If you want to populate VulnerabilityID with the newly created identity .

.then you have to @@Idenity to retrieve the latest identity generated and then send it to insert statement of VulnerabilityiD

For ex:

 declare @vulid int

insert into tablename(params) --> Normal insert statement that generates identity

now, get that latest identity created by using

set @vulid=(select @@Identity) -> newly generated identity is stored in this varaibale now

Insert this into table now..

 updatetablename set vulnerabilityid=@vulid;

This will ensure that latest generated id is inserted as Vulnerabilityid

Hope this helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top