数据库范围内相当于 SET IDENTITY_INSERT OFF
在我的永恒 saga 从 SQL 脚本插入 140 万行数据,我'我们编写了一个基本的 WinForms 应用程序,它获取脚本的每一行并单独执行它。
但是,由于原始脚本包含
SET IDENTITY_INSERT [Table] OFF
并且 SET 是会话范围的命令,因此此设置在每次 SQL 调用时都会丢失,这意味着每一行都会失败。 有没有办法在数据库范围内为整个表设置 IDENTITY_INSERT 关闭,这样我就可以进行这些单独的调用而不会失败? 或者也许我可以通过在每一行附加一个命令来告诉它忽略身份规范?
In my eternal saga to insert 1.4 million rows of data from a SQL script, I've written a basic WinForms app that takes each line of the script and executes it individually.
However, because the original script contained
SET IDENTITY_INSERT [Table] OFF
and SET is a session-wide command, this setting is getting lost on every SQL call, meaning that each line is failing. Is there a way to set IDENTITY_INSERT off for the whole table, database-wide just so I can make these individual calls without them failing? Or perhaps I can tell it to ignore the identity specification by appending a command to each line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信 IDENTITY INSERT 每个会话一次只能覆盖一个表。
您可能必须一次批处理 2 或 300 个插入语句,并在每个批处理之前添加 ident 插入。
所以应用程序将执行这样的块......
I believe that IDENTITY INSERT can only be overridden one table at a time, per session.
You will have to maybe batch up 2 or 300 insert statements at a time and precede each batch with the ident insert.
so the app will execute a block like this....
当这 140 万个插入作业正在进行时,是否还会有其他东西访问/插入到表中。
如果没有,您可以在作业期间简单地禁用 PK 列上的身份吗?
Is anything else going to be accessing/inserting to the table while this is 1.4 million insert job is going on.
If not, you could simply Disable the Identity on the PK column for the duration of the job?
批量插入对您不起作用? 或者 SQL Server 导入/导出向导(此处 或 此处)? 我知道导入/导出可以关闭整个导入的身份插入。 我相当确定您可以在 BULK INSERT 之前执行此操作
BULK INSERT won't work for you? Or the SQL Server Import/Export Wizard (here or here)? I know import/export can turn off identity insert for the whole import. I'm reasonably certain you can do it just prior to BULK INSERT