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

时间:2011-12-01 01:39:00

标签: sql jdbc batch-processing

我正在运行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语句的连接字符串,以避免出现此错误?

4 个答案:

答案 0 :(得分:0)

尝试将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') 

答案 1 :(得分:0)

你可以把上面的代码作为存储过程定义并使用con.prepareStatement传递吗?

答案 2 :(得分:0)

最新更新,以防其他人发现我的调查结果有用......

我最近遇到过这个问题。将查询重新构建为使用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')或在存在中使用另一个脚本作为一种形式消除。

答案 3 :(得分:-1)

你可以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')