sql - 带有合并语句的级联副本导致锁定问题

标签 sql sql-server database tsql merge

SQL Server 2012 (SP1) with 64GB ram using 50GB for max server memory
sp_configure 'locks' = 0
trace flags 1211 & 1224 are off
using READ_COMMITTED
SET LOCK_TIMEOUT 0

在使用 MERGE 时出现此错误:

The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time. Rerun your statement when there are fewer active users. Ask the database administrator to check the lock and memory configuration for this instance, or to check for long-running transactions.

太多的开发人员在合并方面遇到持续的问题,那么我们还能如何有效地为父/子表执行行和相关行的级联复制。例如。 tableA 有很多 tableB 行,tableC 有很多 tableD 行等...

MERGE 在创建新标识值时很有用,同时在同一行上保留旧标识以查看下一个表插入的数据,是否有替代方案?

使用单表插入OUTPUT命令,不能同时获取新旧ID

INSERT INTO tableA
OUTPUT  inserted.[ID],inserted.[col1],inserted.[col2],inserted.col3,
INTO #tmptable

在 SQL Server 中执行此操作的最佳选项是什么?

最佳答案

我最近不得不重新访问此代码以进行更改,我所做的是:code (1) create table #tmptable ([oldID] [bigint] ,[newID] [bigint] ) , (2) 将oldID永久列添加到主表中,这与该行的PK完全相同 (3) INSERT INTO [dbo].[main table] OUTPUT inserted.[oldID],inserted.[newID]进入#tmptable SELECT oldID , column1, column2 ... code - 这是为新 ID 和旧 ID 插入数据以出现在同一行的唯一方法,我很惊讶 msft 没有sql 中的此命令,但作为合并非常慢!

关于sql - 带有合并语句的级联副本导致锁定问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247831/

相关文章:

sql - sql server中如何将列转换为行

sql - 根据标识符将多行展平为单行

c# - 将 nvarchar 转换为数据类型 int 时出错

sql - 将多行合并为 1 行

c# - 默认情况下 dbo 架构中的 EF 6 Code First __MigrationHistory

database - 我应该使用哪个插件进行 hibernate4 的数据库迁移?

sql - MySQL:为表中的每个不同值在外表中的每个键插入一行

java - MySQL 和 Java - 插入表时抛出异常

.net - 如何开始对大型应用程序进行自动化测试?

php - 如何在触发器中将值从一个查询传递到另一个查询