如何在仅具有IDENTITY列的表中插入?


14

给定只有IDENTITY列的表,您如何插入新行?我尝试了以下方法:

INSERT INTO TABLE
(Syntax error)

INSERT INTO TABLE VALUES()
(Syntax error)

INSERT INTO TABLE (Id) VALUES()
(Syntax error)

我正在测试某些东西,只需要IDENTITY列。它不是用于生产。否则,这样的表可以用作序列生成器,而无需其他列。

Answers:



3

另一种方法是使用IDENTITY_INSERT。这样,您可以手动定义要输入的值。就像这样:

SET IDENTITY_INSERT TABLE ON ;

INSERT INTO TABLE (ID) VALUES (1), (2) ;

SET IDENTITY_INSERT TABLE OFF ;

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.