我正在从C#程序执行以下Visual FoxPro SQL语句。我期待获得1个结果,但我没有得到任何结果。
SELECT
iplclaim.lc_lname, ipcname.cn_lname, ipcbusn.cb_bname ;
FROM
iplclaim ;
INNER JOIN
iplclsub ON iplclsub.ls_claimno + iplclsub.ls_sclaimno = iplclaim.lc_claimno + 'A' ;
LEFT OUTER JOIN
ipcname ON ipcname.cn_inqno = iplclsub.ls_inqno ;
LEFT OUTER JOIN
ipcbusn ON ipcbusn.cb_inqno = iplclsub.ls_inqno ;
WHERE
iplclaim.lc_claimno = ' 1105' ;
AND NOT DELETED()
我能够使用以下程序重现从VFP运行的问题...
CLOSE DATABASES all
OPEN DATABASE ipcust.dbc
USE C:\TESTVFP\IPS\DATA\PPDATA\IPlclaim IN 0
USE C:\TESTVFP\IPS\DATA\PPDATA\iplclsub IN 0
USE C:\TESTVFP\IPS\DATA\PPDATA\IPcname IN 0
USE C:\TESTVFP\IPS\DATA\PPDATA\IPcbusn IN 0
SYS(3054,12)
SELECT iplclaim.lc_lname, ipcname.cn_lname, ipcbusn.cb_bname ;
FROM iplclaim ;
INNER JOIN iplclsub ON iplclsub.ls_claimno + iplclsub.ls_sclaimno = ' 1105A' ;
LEFT OUTER JOIN ipcname ON ipcname.cn_inqno = iplclsub.ls_inqno ;
LEFT OUTER JOIN ipcbusn ON ipcbusn.cb_inqno = iplclsub.ls_inqno ;
WHERE iplclaim.lc_claimno = ' 1105' ;
AND NOT DELETED()
似乎如果在执行程序之前表未打开,则结果为零记录。如果我打开表并只执行SQL语句,则返回预期的一条记录。
我不希望的另一件事是,当我运行上面的程序时,SYS(3054,12)返回以下结果(注意“笛卡尔积”)......
SELECT ipcname.cn_lname, ipcbusn.cb_bname FROM iplclsub LEFT OUTER JOIN ipcname ON ipcname.cn_inqno = iplclsub.ls_inqno LEFT OUTER JOIN ipcbusn ON ipcbusn.cb_inqno = iplclsub.ls_inqno WHERE iplclsub.ls_claimno + iplclsub.ls_sclaimno = ' 1105A' AND NOT ip
Using index tag Claimall to rushmore optimize table iplclsub
Rushmore optimization level for table iplclsub: partial
Rushmore optimization level for table ipcname: none
Rushmore optimization level for table ipcbusn: none
Joining table iplclsub and table ipcname (Cartesian product)
Joining intermediate result and table ipcbusn (Cartesian product)
但是当我自己运行SQL语句并且表已经打开时,输出看起来不同(注意没有“Cartisian产品”)
SELECT ipcname.cn_lname, ipcbusn.cb_bname FROM iplclsub LEFT OUTER JOIN ipcname ON ipcname.cn_inqno = iplclsub.ls_inqno LEFT OUTER JOIN ipcbusn ON ipcbusn.cb_inqno = iplclsub.ls_inqno WHERE iplclsub.ls_claimno + iplclsub.ls_sclaimno = ' 1105A' AND NOT ip
Using index tag Claimall to rushmore optimize table iplclsub
Rushmore optimization level for table iplclsub: partial
Rushmore optimization level for table ipcname: none
Rushmore optimization level for table ipcbusn: none
Joining table iplclsub and table ipcname using tag Inqno
Joining intermediate result and table ipcbusn using tag Inqno
最后的注意事项:
iplclaim中只有一条与WHERE
子句匹配的记录。 iplclsub中只有一条与INNER JOIN iplclsub ON
子句匹配的记录。 ipcname中只有一条记录与LEFT OUTER JOIN ipcname ON
子句匹配,ipcbusn中没有与LEFT OUTER JOIN ipbusn ON
子句匹配的记录。
任何人都可以解释造成这个问题的原因以及解决问题的方法吗?
修改
我发现问题是AND NOT DELETED()
。如果我删除该子句,一切都按预期工作。有人可以解释一下,有人可以告诉我另一种排除已删除记录的方法吗?
答案 0 :(得分:2)
There is a warning in the FoxPro documentation about the SELECT-SQL command possibly returning unexpected results when using functions like DELETED() in multiple table queries.
The SET DELETED ON
command can be used to ignore records that are marked for deletion. With that option turned on it looks like the use of the DELETED() function could be removed from the query.
For the C# code, assuming you're using an OLEDB connection, the SET DELETED ON
command can be executed on the same open FoxPro connection prior to executing the query.