I am currently tracking down a rare problem in our SQL generator and was able to narrow down the source of the problem. I created a sample with three testcases to show you the exact behaviour:
QUery.Close;
//Query.SQL.Text := Format('select Options, JobID, StatusMessage from ServerJob where JobID = %d', [ServerJobID]); // #1
//Query.SQL.Text := Format('select JobID, Options, StatusMessage from ServerJob where JobID = %d', [ServerJobID]); // #2
//Query.SQL.Text := Format('select JobID, StatusMessage, Options from ServerJob where JobID = %d', [ServerJobID]); // #3
Query.Open;
try
JobID := Query.FieldByName('JobID').AsInteger; // #1 raises exception
// #2 ok
// #3 ok
StatusMsg := Query.FieldByName('StatusMessage').AsString; // #1 never reaches this point
// #2 raises exception
// #3 ok
except
on Exp: Exception do
begin
ExpMsg := Exp.Message; // #1 Fehler bei einem aus mehreren Schritten bestehenden Vorgang. Prüfen Sie die einzelnen Statuswerte.
// #2 Fehler bei einem aus mehreren Schritten bestehenden Vorgang. Prüfen Sie die einzelnen Statuswerte.
// #3 never reaches this point
Query.Connection.Errors.Count; // #1 1
// #2 1
// #3 never reaches this point
Query.Connection.Errors[0].Description; // #1 Expression illegal in evaluator
// #2 Expression illegal in evaluator
// #3 never reaches this point
end;
end;
The field "Options" is of type XML. So my observation is, that if I reference a field having xml as datatype, all subsequent fields cannot be referenced anymore using TDataset.FieldByName without raising an exception.
Is this some known bug??
P.S. This is the schema of the concerning table:
CREATE TABLE [dbo].[ServerJob](
[JobID] [int] IDENTITY(1,1) NOT NULL,
[PersonID] [int] NULL,
[Name] [varchar](64) NOT NULL,
[Description] [varchar](max) NULL,
[StartTime] [datetime] NOT NULL,
[Cpu] [decimal](5, 2) NULL,
[Ram] [int] NULL,
[Status] [varchar](32) NULL,
[VitalSign] [datetime] NULL,
[Options] [xml] NULL,
[StatusMessage] [varchar](max) NULL,
CONSTRAINT [PK_JobID] PRIMARY KEY CLUSTERED
(
[JobID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I'm using Delphi 10 Seattle and SQL server 2014.