SQL 如何插入只有一个 IDENTITY 列的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/850327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert into a table with just one IDENTITY column?
提问by codeulike
(Came up with this question in the course of trying to answer this other one)
(在尝试回答另一个问题的过程中提出了这个问题)
Consider the following MS-SQL table, called GroupTable:
考虑以下 MS-SQL 表,称为 GroupTable:
GroupID ------- 1 2 3
where GroupID is the primary key and is an Identity column.
其中 GroupID 是主键,是 Identity 列。
How do you insert a new row into the table (and hence generate a new ID) withoutusing IDENTITY_INSERT ON?
如何在不使用 IDENTITY_INSERT ON 的情况下向表中插入新行(从而生成新 ID)?
Note that this:
请注意:
INSERT INTO GroupTable() Values ()
... won't work.
......不会工作。
edit: we're talking SQL 2005 or SQL 2008 here.
编辑:我们在这里谈论 SQL 2005 或 SQL 2008。
回答by DJ.
This should work:
这应该有效:
INSERT INTO GroupTable DEFAULT VALUES
回答by tofi9
Here you go:
干得好:
INSERT INTO GroupTable DEFAULT VALUES
回答by RMK
It is possible to insert more than one row at a time.
一次可以插入多于一行。
For e.g., to insert 30 rows. INSERT INTO GroupTable DEFAULT VALUES GO 30
例如,插入 30 行。INSERT INTO GroupTable DEFAULT VALUES GO 30
This will insert 30 rows by incrementing the identity column each time.
这将通过每次增加标识列来插入 30 行。
回答by Mike Pone
Can you try using a Sequence or something similar? Where you select from a Sequence and it will give you the next value in the sequence.
您可以尝试使用 Sequence 或类似的东西吗?您从序列中选择的位置,它将为您提供序列中的下一个值。