SQL 批量更新错误 - 不允许启动新请求,因为它应该带有有效的事务描述符

发布于 2024-12-19 07:20:49 字数 1127 浏览 1 评论 0原文

我正在运行 microsoft sql 2008 和 jdbc 驱动程序 3.0,并且在批量 SQL 更新时收到此错误

“不允许启动新请求,因为它应该带有有效的事务描述符”

为了调试,我已将批处理大小减少到只有一个语句,但仍然错误。这是

IF EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91') 
    UPDATE staffdetail 
    SET    psnid = 'GC91', 
           servicegroup = '41B001', 
           discipline = 'IT', 
           dob = '1967-09-28', 
           ghdstartdate = '2008-12-15', 
           yearsexperience = '11 to 20 years', 
           classification = 'Admin Officer 1' 
    WHERE  psnid = 'GC91' 
ELSE 
    INSERT INTO staffdetail 
                (psnid, 
                 servicegroup, 
                 discipline, 
                 dob, 
                 ghdstartdate, 
                 yearsexperience, 
                 classification) 
    VALUES      ('GC91', 
                 '41B001', 
                 'IT', 
                 '1967-09-28', 
                 '2008-12-15', 
                 '11 to 20 years', 
                 'Admin Officer 1') 

为什么会出错?我用 google 搜索了这个,只找到了 SQL 2005 中的错误的引用,而不是 2008 中的错误。

有什么方法可以更改用于连接的连接字符串或更改我在批处理中使用的 SQL 语句来尝试避免此错误?

I am running microsoft sql 2008 and jdbc driver 3.0 and am getting this error on a batch SQL update

"New request is not allowed to start because it should come with valid transaction descriptor"

To debug I've reduced the batch size down to just one statement, but it still errors. Here it is

IF EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91') 
    UPDATE staffdetail 
    SET    psnid = 'GC91', 
           servicegroup = '41B001', 
           discipline = 'IT', 
           dob = '1967-09-28', 
           ghdstartdate = '2008-12-15', 
           yearsexperience = '11 to 20 years', 
           classification = 'Admin Officer 1' 
    WHERE  psnid = 'GC91' 
ELSE 
    INSERT INTO staffdetail 
                (psnid, 
                 servicegroup, 
                 discipline, 
                 dob, 
                 ghdstartdate, 
                 yearsexperience, 
                 classification) 
    VALUES      ('GC91', 
                 '41B001', 
                 'IT', 
                 '1967-09-28', 
                 '2008-12-15', 
                 '11 to 20 years', 
                 'Admin Officer 1') 

Why does it error? I've googled this and only found references to a bug in SQL 2005 not 2008.

Is there any way I can change the connection string I use to connect or change the SQL statements I use in the batch to try and avoid this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉睡月亮 2024-12-26 07:20:50

尝试将 SQL 更改为:

UPDATE StaffDetail SET PsnID = 'GC91',ServiceGroup = '41B001',Discipline = 'IT',DOB = '1967-09-28',GHDStartDate = '2008-12-15',YearsExperience = '11 to 20 years',Classification = 'Admin Officer 1' WHERE PsnID = 'GC91';

INSERT INTO StaffDetail (PsnID,ServiceGroup,Discipline,DOB,GHDStartDate,YearsExperience,Classification) 
SELECT 'GC91','41B001','IT','1967-09-28','2008-12-15','11 to 20 years','Admin Officer 1'
WHERE NOT EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91') 

Try changing your SQL to this:

UPDATE StaffDetail SET PsnID = 'GC91',ServiceGroup = '41B001',Discipline = 'IT',DOB = '1967-09-28',GHDStartDate = '2008-12-15',YearsExperience = '11 to 20 years',Classification = 'Admin Officer 1' WHERE PsnID = 'GC91';

INSERT INTO StaffDetail (PsnID,ServiceGroup,Discipline,DOB,GHDStartDate,YearsExperience,Classification) 
SELECT 'GC91','41B001','IT','1967-09-28','2008-12-15','11 to 20 years','Admin Officer 1'
WHERE NOT EXISTS (SELECT * FROM StaffDetail WHERE PsnID = 'GC91') 
感情旳空白 2024-12-26 07:20:50

您可以将上述代码作为存储过程定义并使用 con.prepareStatement 传递吗?

can you put the above code as Stored Procedure definition and pass thru using con.prepareStatement ?

羁拥 2024-12-26 07:20:50

更新较晚,以防其他人发现我的发现有用......

我最近经常遇到这个问题。重新构建查询以使用 CTE 或表变量通常可以解决该问题,但相信最终,如 http://computer-aaaargh.blogspot.co.za/2012/12/weird-cause-of-msg-3989-new-request-is.html ,它必须做与基础数据。查看您的联接并尝试更改您从基础数据中获取的内容 - 在这种情况下,只有一个语句(尝试 SELECT 1 FROM StaffDetail WHERE PsnID = 'GC91')或使用存在中的另一个脚本作为一种形式消除。

A late update, in case others find my findings useful...

I have had this problem a lot recently. Re-architeching the query to either use CTE or Table Variables has often resolved it, but believe that ultimately, as noted at http://computer-aaaargh.blogspot.co.za/2012/12/weird-cause-of-msg-3989-new-request-is.html , it has to do with the underlying data. Look at your joins and try and change what you are fetching back from the underlying data - in this case only the one statement (Try SELECT 1 FROM StaffDetail WHERE PsnID = 'GC91') or use another script in the exist just as a form of elimination.

灼痛 2024-12-26 07:20:50

您可以MERGE 来删除 IFELSE 这使得它实际上是一个语句

MERGE INTO staffdetail AS TARGET 
USING (VALUES ('GC91', 
      '41B001', 
      'IT', 
      '1967-09-28', 
      '2008-12-15', 
      '11 to 20 years', 
      'Admin Officer 1')) AS SOURCE (psnid, servicegroup, discipline, dob, 
      ghdstartdate, yearsexperience, classification) 
ON TARGET.psnid = SOURCE.psnid 
WHEN MATCHED THEN 
  UPDATE SET servicegroup = SOURCE.servicegroup, 
             discipline = SOURCE.discipline, 
             dob = SOURCE.dob, 
             ghdstartdate = SOURCE.ghdstartdate, 
             yearsexperience = SOURCE.yearsexperience, 
             classification = SOURCE.classification 
WHEN NOT MATCHED BY TARGET THEN 
  INSERT (psnid, 
          servicegroup, 
          discipline, 
          dob, 
          ghdstartdate, 
          yearsexperience, 
          classification) 
  VALUES ('GC91', 
          '41B001', 
          'IT', 
          '1967-09-28', 
          '2008-12-15', 
          '11 to 20 years', 
          'Admin Officer 1') 

You can MERGE instead which removes the IF and ELSE which makes it really one statement

MERGE INTO staffdetail AS TARGET 
USING (VALUES ('GC91', 
      '41B001', 
      'IT', 
      '1967-09-28', 
      '2008-12-15', 
      '11 to 20 years', 
      'Admin Officer 1')) AS SOURCE (psnid, servicegroup, discipline, dob, 
      ghdstartdate, yearsexperience, classification) 
ON TARGET.psnid = SOURCE.psnid 
WHEN MATCHED THEN 
  UPDATE SET servicegroup = SOURCE.servicegroup, 
             discipline = SOURCE.discipline, 
             dob = SOURCE.dob, 
             ghdstartdate = SOURCE.ghdstartdate, 
             yearsexperience = SOURCE.yearsexperience, 
             classification = SOURCE.classification 
WHEN NOT MATCHED BY TARGET THEN 
  INSERT (psnid, 
          servicegroup, 
          discipline, 
          dob, 
          ghdstartdate, 
          yearsexperience, 
          classification) 
  VALUES ('GC91', 
          '41B001', 
          'IT', 
          '1967-09-28', 
          '2008-12-15', 
          '11 to 20 years', 
          'Admin Officer 1') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文