使用nvarchar(max)

问题描述 投票:0回答:1

我们遇到了我无法解释的行为。对于上下文: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.]

现在我想出的是:

    当@Parameter阵列相对较大时,问题提出了(41个报告和47个报告)。使用1-2-3不会创建问题
  1. i将问题范围缩小到一列(!),请参见标记(*)和(**)的代码。如果我做列文本
  2. nvarchar(200)

    都可以正常工作。

    试图玩耍,我们找到了另一个解决方法:
  3.     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
  4. 到目前为止,这些都是解决方案,而不是解决方案。重新启动SQL Server无济于事。 我很明显的问题是 - 有人观察还是有一个想法,为什么会发生这种情况? thanks.

僵局仅涉及一个带有不同“执行上下文ID”的会议,这是“ Query Query Parallelisp僵局”。 这些总是产品问题。 如果它重复出现在最新的服务包上,请打开一个支持案例,并暂时围绕它进行工作。

请参见:Https://dba.stackexchange.com/questions/216086/solving-intra-parallel-parallel-query-deadlocksenter image description here

sql sql-server t-sql transactions
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.