原始sql指令可以查出我想要得資料

===================================

select * from table
where TempId like 'TpTrans%'
and FinalTime BETWEEN '2008/11/10 00:00' and '2008/11/13 00:00'
order by FinalTime desc

===================================

使用DELPHI來實現使用者輸入資料條件來查詢資料並輸出結果

===================================

//依序比對:   資料庫欄位    對應查詢變數        說明
//            Column01      Q_CustId.text       客戶代碼
//            Column02      Q_TrpType.text      運輸類別
//            Column03      Q_DeclNo.text       轉檔號碼
//            Column04      Q_CurCode.text      幣別
//            Column05      Q_DutyRateP.text    金額
//            FinalUser     Q_FinalUser.text    轉檔人
//            FinalTime     StartDate           起始日期
//                          StartTime           起始時間
//                          EndDate             結束日期
//                          EndTime             結束時間
procedure TBrCommonForm.Q_QueryBtnClick(Sender: TObject);
var
CTrpType:String; {運輸類別只取中文字暫存變數}
StartDate:String; {起始日期}
StartTime:String; {起始時間}
EndDate:String; {結束日期}
EndTime:String; {結束時間}
StartDateTime:String; {起始時間與起始日期合併後查詢用}
EndDateTime:String; {結束時間與結束日期合併後查詢用}
DutyRateCKB:String; {金額大於小於等於選項變數}
begin
  DFQuery1.SQL.Clear;
  DFQuery1.SQL.Add('select * from table');
  DFQuery1.SQL.Add('where TempID like :E_TempID');
  if Q_CustId.text <> '' then //客戶代碼
  begin
    DFQuery1.SQL.Add('And Column01 = :E_CustId');
    DFQuery1.Parameters.ParamByName('E_CustId').Value := Q_CustId.text;
  end;

  if Q_TrpType.text <> '' then //運輸類別
  begin
    CTrpType := Copy(Trim(Q_TrpType.text),3,8); //運輸類別只取中文字比對
    DFQuery1.SQL.Add('And Column02 = :E_TrpType');
    DFQuery1.Parameters.ParamByName('E_TrpType').Value := CTrpType;
  end;

  if Q_DeclNo.text <> '' then //轉檔號碼
  begin
    DFQuery1.SQL.Add('And Column03 = :E_DeclNo');
    DFQuery1.Parameters.ParamByName('E_DeclNo').Value := Trim(Q_DeclNo.text);
  end;

  if Q_CurCode.text <> '' then //幣別
  begin
    DFQuery1.SQL.Add('And Column04 = :E_CurCode');
    DFQuery1.Parameters.ParamByName('E_CurCode').Value := Trim(Q_CurCode.text);
  end;

  if Q_DutyRateP.Text <> '' then //金額
  begin
    DutyRateCKB:=Trim(Q_DutyRateCKB.Items[Q_DutyRateCKB.ItemIndex]);
    DFQuery1.SQL.Add('And Column05 :E_DutyRateCKB :E_DutyRateP');
    DFQuery1.Parameters.ParamByName('E_DutyRateP').Value := Trim(Q_DutyRateP.text);
    DFQuery1.Parameters.ParamByName('E_DutyRateCKB').Value := DutyRateCKB;
  end;

  if Q_FinalUser.text <> '' then //轉檔人
  begin
    DFQuery1.SQL.Add('And FinalUser = :E_FinalUser');
    DFQuery1.Parameters.ParamByName('E_FinalUser').Value := Trim(Q_FinalUser.text);
  end;

  if Q_DateCheckBox.Checked then //核選依日期做搜尋
  begin
    StartDate := FormatDateTime('yyyy/mm/dd',Q_StartDate.Date); //起始日期格式設定
    StartTime := FormatDateTime('hh:nn',Q_StartTime.Time); //起始時間設定
    EndDate := FormatDateTime('yyyy/mm/dd',Q_EndDate.Date); //結束日期格式設定
    EndTime := FormatDateTime('hh:nn',Q_EndTime.Time); //結束時間格式設定
    StartDateTime := StartDate+' '+StartTime; //起始時間整合成與資料庫相同格式的字串模式比對
    EndDateTime := EndDate+' '+EndTime; //結束時間整合成與資料庫相同格式的字串模式比對
    DFQuery1.SQL.Add('And FinalTime BETWEEN :E_StartDateTime and :E_EndDateTime');
    DFQuery1.Parameters.ParamByName('E_StartDateTime').Value := StartDateTime;
    DFQuery1.Parameters.ParamByName('E_EndDateTime').Value := EndDateTime;
  end;

  DFQuery1.SQL.Add('order by FinalTime desc');
  DFQuery1.Parameters.ParamByName('E_TempID').Value := 'TpTrans%';
  DFQuery1.Open;
  SearchPanel.Visible := false; //查詢完畢關閉查詢視窗
end;

======================================

arrow
arrow
    全站熱搜

    NoMoney NoHoney 發表在 痞客邦 留言(0) 人氣()