[只是分享...]很久以前,DB没有参考约束,有人从PK-autoincrementing表中强制删除了一个职员,这将不允许PK关闭。孤立了一堆数据,我不得不重新插入与以前的PK值的行。数据库不允许表结构更改,但允许重命名。TSQL重新插入已删除的行w/a PK
0
A
回答
0
不知道你的意思是自动递增或为什么它不允许'PK关闭',我们不能真正提出一个不同的解决方案。
如果通过自动递增来表示IDENTITY,那么您可以使用SET IDENTITY_INSERT OFF
来允许显式插入标识值。
0
这里就是我所做的:
/****
create protoTable w/ same structure as your mainTable that has the data you are trying to fix in this example the fieldname of the primary key is FldPK
assumption here is that your primary key is getting auto incremented
get a list of the fields in the mainTable that are NOT NULL.
in this example those fields are <not null fields>
get a list of all fields in the mainTable
in this example <all fields>, rather than spell out the fields. DO NOT INCLUDE the primary key field name
***/
declare @x int, @y int, @iLast int
select @iLast = (select MAX(FldPK) from mainTable)
set @x = 1
while @x <= @iLast
begin
select @y = (select COUNT(*) from mainTable where FldPK = @x)
if @y = 1
begin
insert into protoTable(<all fields>)
select <all fields> from mainTable where FldPK = @x
end
else
begin
insert into protoTable (<not null fields>)values('N','xyz'+convert(varchar,@x)) /*or whatever values are valid to fulfill not null*/
/* this is where you keep one or more of the missing rows to update later with the lost data */
if @x <> 126
begin
delete protoTable where FldPK = @x
end
end
set @[email protected]+1
end
然后改名为mainTable归档和protoTable到mainTable。如果任何人有一个这样做的狡猾的方式,我很想看到它。
相关问题
- 1. tsql:删除行为
- 2. 带有关系的已删除对象由AFIncremntalStore重新插入
- 3. TSQL更新触发器:加入插入和删除
- 4. Python/sqlite:获取新插入行的PK
- 5. Cassandra - 重新插入删除的列
- 6. TSQL删除重复的行只
- 7. SQL插入 - 已删除表
- 8. 散列:插入已删除的插槽
- 9. TSQL插入和删除重复项以提高查询性能
- 10. hibernate @ManyToMany删除并重新插入
- 11. 删除并重新插入ListView列(.NET)
- 12. 值删除后不会重新插入
- 13. Mysql主键删除和重新插入
- 14. 删除行,重新编辑非PK索引列? C#
- 15. SQL Server重复使用已删除的身份PK
- 16. JPA错误“无法合并已删除的实体”试图删除并重新插入一行SpringData
- 17. 从JunctionTable更新/删除TSQL
- 18. 删除pk场
- 19. 插入/删除行时更新表格
- 20. TSQL合并插入重复
- 21. TSQL - 选择插入的行
- 22. 更新/删除/插入DataGridView
- 23. 更新并插入TSQL
- 24. 使用Pelops在cassandra中删除行后重新插入数据
- 25. tsql - 删除最后一行
- 26. SQL Server 2008同步 - 如何用新主键重新插入冲突的pk行
- 27. SQLAlchemy的PK插入
- 28. 使用临时表将已删除的数据重新插入数据集。 Qlikview
- 29. 触发插入行插入删除
- 30. 删除插入目标表中已有的新记录
这种情况涉及到遗留系统,所以我不知道底层设置。当我使用Design并且在PK身份列上尝试将身份规范(身份)从“是”更改为“否”时,数据库将使用符号排除“保存”:不允许保存更改。您所做的更改需要删除并重新创建或启用以下表格[原文如此]选项预防保存需要重新创建表格的更改.'并列出我刚刚尝试修改的表格 – gordon
在在SSMS中查询窗口,键入'SET IDENTITY_INSERT YourTable ON'。您现在将被允许更新并在您的标识列中插入值。完成后,键入“SET IDENTITY_INSERT YourTable OFF”,该字段将再次自动增加。这不是特定的数据库设置,而是您发布的命令。你绝对无法从设计窗格中做任何事情。你为什么不做一些研究并提供更多信息,那么我们可以进一步提供帮助。 –