SQL-如何在不自动递增 ID 列的情况下插入行?

发布于 2024-07-25 17:49:17 字数 246 浏览 4 评论 0原文

我有一个表,其中有一个强制自动增量列,该列是一个非常有价值的 ID,在整个应用程序中都会保留。 很抱歉,我的开发很差,让它成为自动递增列。

所以,问题就在这里。 我必须将已创建并从表中删除的列的 ID 插入到该表中。 有点像复活这个 ID 并将其放回表中。

那么我怎样才能以编程方式做到这一点而不关闭列增量呢? 如果我错了,请纠正我,如果我以编程方式关闭它,它将在 0 或 1 处重新启动,我不希望这种情况发生......

I have a table that has a forced auto increment column and this column is a very valuable ID that is retained through out the entire app. Sorry to say it was poor development on my part to have this be the auto incrementing column.

So, here is the problem. I have to insert into this table an ID for the column that has already been created and removed from the table. Kind of like resurrecting this ID and putting it back into the table.

So how can I do this programatically do this without turning the column increment off. Correct me if I am wrong, if I turn it off programatically, It will restart at 0 or 1 and I don't want that to happen...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的旅程 2024-08-01 17:49:17

如果您使用的是 Microsoft SQL Server,则可以通过发出语句 Set Identity_Insert [TableName] On 来“关闭”自动增量功能,如下所示:

  Set Identity_Insert [TableName] On
  -- --------------------------------------------
  Insert TableName (pkCol, [OtherColumns])
  Values(pkValue, [OtherValues])
  -- ---- Don't forget to turn it back off ------
  Set Identity_Insert [TableName] Off

If you are in Microsoft SQL Server, you can "turn off" the autoIncrementing feature by issuing the statement Set Identity_Insert [TableName] On, as in:

  Set Identity_Insert [TableName] On
  -- --------------------------------------------
  Insert TableName (pkCol, [OtherColumns])
  Values(pkValue, [OtherValues])
  -- ---- Don't forget to turn it back off ------
  Set Identity_Insert [TableName] Off
远山浅 2024-08-01 17:49:17

除了 Charles 的答案(现在 100% 正确:-) 并保留表上 IDENTITY 的当前值)之外,您可能还想检查表上 IDENTITY 的当前值 - 您可以这样做在此使用此命令:

DBCC CHECKIDENT('YourTableName')

如果您需要实际更改它,您可以在此处使用此命令:

DBCC CHECKIDENT ('YourTableName', RESEED, (new value for IDENTITY) )

In addition to Charles' answer (which is now 100% correct :-) and which preserves the current value of the IDENTITY on the table), you might also want to check the current value of an IDENTITY on a table - you can do this with this command here:

DBCC CHECKIDENT('YourTableName')

If you ever need to actually change it, you can do so by using this command here:

DBCC CHECKIDENT ('YourTableName', RESEED, (new value for IDENTITY) )
待天淡蓝洁白时 2024-08-01 17:49:17

实际上,上面的 INDENTITY_INSERT 代码是正确的 - 将其打开告诉服务器您要自己插入值。 它允许您将值插入到 IDENTITY 列中。 然后,您希望在完成后将其关闭(允许服务器生成并插入值)。

Actually, the code above for INDENTITY_INSERT is correct - turning it ON tells the server you want to insert the values yourself. It allows you to insert values into an IDENTITY column. You then want to turn it back off (allowing the server to generate and insert the values) when you are done.

夏夜暖风 2024-08-01 17:49:17
bulk insert tablename from 'C:\test.csv' with (rowterminator = '\n',fieldterminator = ',',KEEPIDENTITY)
bulk insert tablename from 'C:\test.csv' with (rowterminator = '\n',fieldterminator = ',',KEEPIDENTITY)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文