G
gzmich
Unregistered / Unconfirmed
GUEST, unregistred user!
编写的SQL查询为何不能运行
请看以下源程序
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.BitBtn2Click(Sender: TObject);
var E,R,Date1,Date2:string;
begin
TRY
Date1:=FormatDateTime('yyyy-mm-dd',DateTimePicker1.Date);
Date2:=FormatDateTime('yyyy-mm-dd',DateTimePicker2.Date);
Query1.CLOSE;
Query1.SQL.CLEAR;
Query1.SQL.ADD('SELECT DISTINCT ');
Query1.SQL.ADD('IV.TRANSACTION_ID,');
Query1.SQL.ADD('IV.WORKORDER_TYPE,');
Query1.SQL.ADD('OP.WORKORDER_BASE_ID,');
Query1.SQL.ADD('OP.WORKORDER_LOT_ID,');
Query1.SQL.ADD('OP.WORKORDER_SPLIT_ID,');
Query1.SQL.ADD('IV.PART_ID,');
Query1.SQL.ADD('IV.QTY,');
Query1.SQL.ADD('IV.DESCRIPTION,');
Query1.SQL.ADD('IV.TRANSACTION_DATE');
Query1.SQL.ADD('FROM');
Query1.SQL.ADD('SYSADM_INVENTORY_TRANS IV,');
Query1.SQL.ADD('SYSADM_OPERATION OP');
Query1.SQL.ADD('WHERE');
Query1.SQL.ADD('(IV.WORKORDER_SPLIT_ID=OP.WORKORDER_SPLIT_ID)');
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID=OP.WORKORDER_BASE_ID) ');
Query1.SQL.add(' AND (IV.WORKORDER_LOT_ID=OP.WORKORDER_LOT_ID)');
Query1.SQL.ADD(' AND (OP.SEQUENCE_NO='+#39+'%10'+#39+') ');
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID IS NOT NULL)');
R:=QUERY2.FIELDBYNAME('ID').VALUE;
QUERY1.SQL.ADD(' AND (OP.RESOURCE_ID='+#39+R+#39+')');
E:='GECL';
TABLE1.SETKEY;
TABLE1.FIELDBYNAME('ID').ASSTRING:=E;
IF TABLE1.GOTOKEY THEN
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID LIKE '+#39+'G%'+#39+')')
ELSE
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID NOT LIKE '+#39+'G%'+#39+')');
Query1.SQL.ADD(' AND (IV.TRANSACTION_DATE BETWEEN '+#39+DATE1+#39+' AND '+#39+DATE2+#39+') ');
Query1.OPEN;
except
query1.close;
exit;
end;
end;
请看以下源程序
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.BitBtn2Click(Sender: TObject);
var E,R,Date1,Date2:string;
begin
TRY
Date1:=FormatDateTime('yyyy-mm-dd',DateTimePicker1.Date);
Date2:=FormatDateTime('yyyy-mm-dd',DateTimePicker2.Date);
Query1.CLOSE;
Query1.SQL.CLEAR;
Query1.SQL.ADD('SELECT DISTINCT ');
Query1.SQL.ADD('IV.TRANSACTION_ID,');
Query1.SQL.ADD('IV.WORKORDER_TYPE,');
Query1.SQL.ADD('OP.WORKORDER_BASE_ID,');
Query1.SQL.ADD('OP.WORKORDER_LOT_ID,');
Query1.SQL.ADD('OP.WORKORDER_SPLIT_ID,');
Query1.SQL.ADD('IV.PART_ID,');
Query1.SQL.ADD('IV.QTY,');
Query1.SQL.ADD('IV.DESCRIPTION,');
Query1.SQL.ADD('IV.TRANSACTION_DATE');
Query1.SQL.ADD('FROM');
Query1.SQL.ADD('SYSADM_INVENTORY_TRANS IV,');
Query1.SQL.ADD('SYSADM_OPERATION OP');
Query1.SQL.ADD('WHERE');
Query1.SQL.ADD('(IV.WORKORDER_SPLIT_ID=OP.WORKORDER_SPLIT_ID)');
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID=OP.WORKORDER_BASE_ID) ');
Query1.SQL.add(' AND (IV.WORKORDER_LOT_ID=OP.WORKORDER_LOT_ID)');
Query1.SQL.ADD(' AND (OP.SEQUENCE_NO='+#39+'%10'+#39+') ');
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID IS NOT NULL)');
R:=QUERY2.FIELDBYNAME('ID').VALUE;
QUERY1.SQL.ADD(' AND (OP.RESOURCE_ID='+#39+R+#39+')');
E:='GECL';
TABLE1.SETKEY;
TABLE1.FIELDBYNAME('ID').ASSTRING:=E;
IF TABLE1.GOTOKEY THEN
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID LIKE '+#39+'G%'+#39+')')
ELSE
Query1.SQL.ADD(' AND (IV.WORKORDER_BASE_ID NOT LIKE '+#39+'G%'+#39+')');
Query1.SQL.ADD(' AND (IV.TRANSACTION_DATE BETWEEN '+#39+DATE1+#39+' AND '+#39+DATE2+#39+') ');
Query1.OPEN;
except
query1.close;
exit;
end;
end;