我们遇到了我无法解释的行为。对于上下文:SQL Server 2022(V16.00.1135)。 存储过程具有以下代码:
begin try
begin transaction
drop table if exists #reports;
drop table if exists #details;
select
reportId
, reportName
, details
into #reports
from openjson(@parameter) with (
reportId int '$.reportId'
, reportName nvarchar(100) '$.reportName'
, details nvarchar(max) '$.reportDetails' as json
)
select
#reports.reportId
, details.detailId
, details.text -- (*)
into #details
from #reports
outer apply openjson(#reports.details) with (
detailId nvarchar(50) '$.detailId'
, text nvarchar(max) '$.text' -- (**)
) as details
-- acutal data processing goes here
select * from #reports
select * from #details
-- further create/update/... records in tables
commit transaction
end try
begin catch
-- process error ...
rollback transaction;
end catch;
数据的一个示例是:
declare @parameter nvarchar(max) = '[
{
"reportId": 1,
"reportName": "repN",
"reportDetails": [
{"detailId": "9", "text": "aaa"},
{"detailId": "8", "text": "bbb"}
]
},
{
"reportId": 2,
"reportName": "repII",
"reportDetails": []
}
]'
The result is:
reportId reportName details
1 repN "[ ..."
2 repII "[]"
reportId detailId text
1 a 9
1 b 8
2 (null) (null)
上面是预期的。
这是一些简化的代码 - 在实际代码中,我在“报告”中有25列,大约30列详细说明。在某些情况下,我收到以下错误消息:
2 Procedure ...name... .
Error 1205, Severity 13, State 78, Line 114,
Message: [Transaction (Process ID 51) was deadlocked on lock |
communication buffer resources with another process and has been
chosen as the deadlock victim. Rerun the transaction.]
现在我想出的是:
nvarchar(200)
都可以正常工作。
试图玩耍,我们找到了另一个解决方法:
declare @td as table (
reportId int
, detailId int
, text nvarchar(max)
)
insert into @td
select
#reports.reportId
, details.detailId
, details.text
from #reports
outer apply openjson(#reports.details) with (
detailId nvarchar(50) '$.detailId'
, text nvarchar(max) '$.text'
) as details
僵局仅涉及一个带有不同“执行上下文ID”的会议,这是“ Query Query Parallelisp僵局”。 这些总是产品问题。 如果它重复出现在最新的服务包上,请打开一个支持案例,并暂时围绕它进行工作。
请参见:Https://dba.stackexchange.com/questions/216086/solving-intra-parallel-parallel-query-deadlocks