Mort Mort - 3 months ago 24
SQL Question

SQL Batch Update error - New request is not allowed to start because it should come with valid transaction descriptor

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?

Answer

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') 
Comments