【发布时间】:2022-01-16 10:49:37
【问题描述】:
我通过 SQL Server 执行查询以从 MySQL 数据库中检索数据,如下所示:
SELECT * FROM OPENQUERY(MyLinkedServerNameToMySQL,
'SELECT actor_id, first_name, last_Name, last_update
FROM sakila.actor')
其中last_update 列数据类型为timestamp。
通过Microsoft ADODataSet库执行查询并检索DataSet中的数据后,DataSet中last_update的数据类型为nvarchar,而我期望datetime。
谁能解释为什么会出现这个问题?
更新:
这是我的代码:
unit GetDataFormMySQLU;
interface
procedure GetDataFromMySQL;
implementation
uses DB, ADODB, Variants;
procedure DoSomthingForBoolean(AValue: Variant);
begin
end;
procedure DoSomthingForDateTime(AValue: Variant);
begin
end;
procedure DoSomthingForNumber(AValue: Variant);
begin
end;
procedure DoSomthingForString(AValue: Variant);
begin
end;
procedure GetDataFromMySQL;
var
LADOConn: TADOConnection;
LDS: TADODataSet;
i: Integer;
LValue: Variant;
begin
LADOConn := TADOConnection.Create(nil);
try
//:
LDS := TADODataSet.Create(nil);
try
LDS.Connection := LADOConn;
LDS.CommandText := 'SELECT * FROM ' +
'OPENQUERY(MyLinkedServerNameToMySQL, ' +
'''SELECT actor_id, first_name, last_Name, last_update ' +
'FROM sakila.actor'')';
LDS.Open;
while not LDS.Eof do
begin
for i := 0 to LDS.Fields.Count - 1 do
begin
LValue := LDS.Fields[i].Value;
case LDS.Fields[i].DataType of
ftBoolean: DoSomthingForBoolean(LValue);
ftDate,
ftTime,
ftDateTime: DoSomthingForDateTime(LValue); (*this branch will not be invoked for
timestamp or datetime column of MySQL*)
ftSmallint,
ftInteger,
ftWord,
ftFloat,
ftCurrency,
ftBCD,
ftAutoInc,
ftLongWord,
ftShortint,
ftExtended,
ftSingle: DoSomthingForNumber(LValue);
else DoSomthingForString(LValue); (*actually timestamp and datetime data type of MySQL
will be detected as ftWideString(nvarchar) and this
branch will be invoked*)
end;
end;
LDS.Next;
end;
finally
LDS.Free;
end;
finally
LADOConn.Free;
end;
end;
end.
【问题讨论】:
-
可能是因为
TIMESTAMP Transact-SQL timestamp data type is different from the timestamp data type defined in the ISO standard. It has nothing to do with date and time. Timestamp is a synonym for rowversion. -
您可以尝试使用测试表执行相同的查询,但将
timestamp更改为datetime并检查它是否产生相同的错误? -
如何得出这个专栏是
nvarchar的结论? -
@Luuk 在我的代码中,我检查了每一列的数据类型
-
顺便说一句:linked_server 周围不应有引号,请参阅OPENQUERY中的示例
标签: mysql sql sql-server delphi linked-server