【发布时间】:2014-03-26 02:57:57
【问题描述】:
我正在处理 INSERT 查询,该查询在名为 VulnerabilityAlertDocument
的表中插入新行此表有 3 个字段:
Id:这是一个自动增量 int,它是我的PRIMARY KEY
VulnerabilityAlertId:此字段为 int,必须与 **Id 列的值相同
SourceId:是一个 varchar(50) 并包含一些文本。
现在我的问题是如何使 VulnerabilityAlertId 具有与此新记录的 *auto increment Id 值相同的值。
如果我这样做:
INSERT INTO VulnerabilityAlertDocument
( [VulnerabilityAlertId], [SourceId] )
VALUES
(4, 'TEST');
它在表中创建一条新记录,其中 Id 列值未指定,因为它是自动递增的,并且 VulnerabilityAlertId 值为 4
我需要在此查询中,VulnerabilityAlertId 值自动设置为新行的 Id 值
我该怎么做?
【问题讨论】:
-
你想通过一个查询来实现这个吗?你能把它分成 2 个查询吗?
-
据我所知,您必须编写两个查询或触发相同的查询。如果有任何问题,请告诉我。
-
VulnerabilityAlertId的值ever 是否会与Id不同?我假设(但不确定)您想在此处设置“默认”值? -
这个问题和What can I do to set the value of a column in a new row in such a way that it have the same value of the auto increment primary key of the table?有什么区别?此外,这闻起来像 XY 问题。 为什么您需要始终包含相同值的两列?
-
简单地将 VulnerabilityAlertId 设为计算列。
标签: sql sql-server database rdbms