SQL Server Case语句在Delphi TADODataSet中不起作用

时间:2015-08-24 09:37:07

标签: sql-server delphi ado

在Delphi XE5中,我使用TADODataSet并将其CommandText设置为此命令(使用CASE

SELECT 
    Master.*,
    CASE
       ( SELECT TOP (1) personeli_State 
         FROM Detail 
         WHERE Detail.FK_Key = Master.pk_key 
           AND Detail.personeli_State = 'Test' 
           AND Detail.sended = 0 ) 
       WHEN 'Test' THEN 'Test exist'
       ELSE '' 
     END AS PersonState
FROM   
    Master
LEFT OUTER JOIN 
    ExtraInfo ON ExtraInfo.p_key = Master.fk_ExtraInfo
WHERE  
    (fk_key=:Fk)

记录集为空,但没有CASE 语句记录集的数据。 (我不能使用计算列或SQL Server视图,因为我使用了动态查询)

1 个答案:

答案 0 :(得分:1)

将CASE语句中的Select部分更改为:

    CASE
       ( SELECT TOP (1) personeli_State 
         FROM Detail 
          WHERE Detail.personeli_State = 'Test' 
           AND Detail.sended = 0
           AND Detail.FK_Key = Master.pk_key ) 

在我看来,似乎ADO改变了执行计划的优先级......!