5,388
社区成员
发帖
与我相关
我的任务
分享
Function TCustomQueryForm.FunGetYOrderSql: String;
Var
AStrSQL: String; // -- 主语句
AstrOrder, AFilter, AWhereStr: String;
AMaxRecordcount: Integer;
Function GetMaxID(Aord: integer): String;
Var
ALine, AStr: String;
Begin
ALine := '1000';
Case Aord Of
0:
Begin
ALine := inttostr((FInfoPageIndex_Int) * FInfoPageSize_Int);
AStr := 'select max(' + FInfoOrderBy_str + ') as AMaxID from (select top ' + ALine + ' * from ' +
FQueryTableName + AWhereStr + ' order by ' + FInfoOrderBy_str + ')as mytable';
End;
1:
Begin
ALine := inttostr((FInfoPageIndex_Int - 1) * FInfoPageSize_Int);
AStr := 'select min(' + FInfoOrderBy_str + ') as AMaxID from (select top ' + ALine + ' * from ' +
FQueryTableName + AWhereStr + ' order by ' + FInfoOrderBy_str + ' desc )as mytable';
End;
End;
GetDataset(AStr, ADQ_Count);
Result := ADQ_Count.fieldbyname('AMaxID').AsString;
End;
Function GetMinID(Aord: integer): String;
Var
ALine, AStr: String;
Begin
ALine := '1000';
Case Aord Of
0:
Begin
ALine := inttostr((FInfoPageIndex_Int - 1) * FInfoPageSize_Int);
AStr := 'select max(' + FInfoOrderBy_str + ') as AMaxID from (select top ' + ALine + ' * from ' +
FQueryTableName + AWhereStr + ' order by ' + FInfoOrderBy_str + ')as mytable';
End;
1:
Begin
ALine := inttostr((FInfoPageIndex_Int) * FInfoPageSize_Int);
AStr := 'select min(' + FInfoOrderBy_str + ') as AMaxID from (select top ' + ALine + ' * from ' +
FQueryTableName + AWhereStr + ' order by ' + FInfoOrderBy_str + ' desc )as mytable';
End;
End;
GetDataset(AStr, ADQ_Count);
Result := ADQ_Count.fieldbyname('AMaxID').AsString;
End;
Begin
Try
AFilter := FInfoWhere_Str;
If FIfQueryOfDate Then
Begin
If (Trim(FFieldofDate) <> '') And (Trim(DateEdtBegin.Text) <> '') And (Trim(DateEdtEnd.Text) <> '') Then
AFilter := AFilter + ' and ' + FFieldofDate + '>=' + QuotedStr(formatdatetime('yyyy-mm-dd', DateEdtBegin.Date)) +
' and ' + FFieldofDate + '<=' + QuotedStr(formatdatetime('yyyy-mm-dd', DateEdtEnd.Date));
End;
If QueryParamEditor.FilterText <> '' Then
AFilter := AFilter + ' and (' + QueryParamEditor.FilterText + ')';
Case FCurSubQueryType Of
1:
Begin
If SubQueryParam_DCL <> '' Then
AFilter := AFilter + ' and ' + self.SubQueryParam_DCL;
End;
2:
Begin
If SubQueryParam_YCL <> '' Then
AFilter := AFilter + ' and ' + self.SubQueryParam_YCL;
End;
End; //case FCurSubQueryType of
If Trim(FQueryParam) <> '' Then
Begin
AWhereStr := FQueryParam + ' and ' + AFilter;
AFilter := trim(UpperCase(AWhereStr));
AFilter := Copy(AFilter, Pos(AFilter, 'WHERE') + 6, Length(AFilter)); // FQueryParam
End
Else
AWhereStr := ' where ' + AFilter;
If FInfoOrderType_str <> '0' Then
Begin
AstrOrder := ' order by ' + FInfoOrderBy_str + ' desc';
End
Else
Begin
AstrOrder := ' order by ' + FInfoOrderBy_str + ' asc';
End;
If Not FWhetherDetachPage Then
Begin
AMaxRecordcount := 10000;
AStrSQL := 'select top ' + inttostr(AMaxRecordcount) + ' * from ' + FQueryTableName;
AStrSQL := AStrSQL + AWhereStr + AstrOrder;
result := AstrSQL;
Exit;
End;
FunGetPageCount(AFilter);
If FInfoOrderType_str = '0' Then
Begin
AStrSQL := 'select * from ' + FQueryTableName + AWhereStr;
If FInfoPageIndex_Int <> 1 Then
AStrSQL := AStrSQL + ' and ' + FInfoOrderBy_str + ' > ' + GetMinID(0);
If FInfoPageIndex_Int <> FPageCount Then
AStrSQL := AStrSQL + ' and ' + FInfoOrderBy_str + ' <= ' + GetMaxID(0);
End
Else
Begin
AStrSQL := 'select * from ' + FQueryTableName + AWhereStr;
If FInfoPageIndex_Int <> 1 Then
AStrSQL := AStrSQL + ' and ' + FInfoOrderBy_str + ' <= ' + GetMaxID(1);
If FInfoPageIndex_Int <> FPageCount Then
AStrSQL := AStrSQL + ' and ' + FInfoOrderBy_str + ' > ' + GetMinID(1);
End;
result := AStrSQL;
Except
result := '';
End;
End;
Function TCustomQueryForm.FunGetDetachPageSql: String;
Var
AStrSQL: String; // -- 主语句
AStrTmp: String; //-- 临时变量
AStrOrder, AWhereStr, AFilter: String;
AMaxRecordcount: integer;
Begin
AFilter := FInfoWhere_Str;
If Trim(FQueryParam) <> '' Then
Begin
AWhereStr := FQueryParam + ' and ' + AFilter;
AFilter := trim(UpperCase(AWhereStr));
AFilter := Copy(AFilter, Pos(AFilter, 'WHERE') + 6, Length(AFilter)); // FQueryParam
End
Else
AWhereStr := ' where ' + AFilter;
Try
FunGetPageCount(AFilter);
If FInfoOrderType_str <> '0' Then
Begin
AstrOrder := ' order by ' + FInfoOrderBy_str + ' desc';
End
Else
Begin
AstrOrder := ' order by ' + FInfoOrderBy_str + ' asc';
End;
AStrSQL := 'Select * FROM (' +
'select ' + FInfoFldCow_str + ', ROW_NUMBER()Over(' + AStrOrder + ') as ZYL_RowId ' +
' from ' + FQueryTableName + AWhereStr + ' ' + FInfoGroupBy_str +
' ) as mytable where ZYL_RowId between ' + inttostr((FInfoPageIndex_Int - 1) * FInfoPageSize_Int + 1) +
' and ' + inttostr(FInfoPageIndex_Int * FInfoPageSize_Int) + AstrOrder;
result := AstrSQL;
Except
result := '';
End;
End;
Function TCustomQueryForm.FunGetPageCount(AFilterStr: String): Boolean;
Var
ASqlStr: String;
Begin
Try
If FInfoPageSize_Int = 0 Then Exit;
ASqlStr := 'Select Max(ZYL_RowId) as ZYL_RowId FROM (' +
'select ' + FInfoFldCow_str + ', ROW_NUMBER()Over( order by ' + FInfoOrderBy_str + ') as ZYL_RowId ' +
' from ' + FQueryTableName + ' where ' + AFilterStr + ' ' + FInfoGroupBy_str +
' ) as mytable';
// ASqlStr := 'Select count(' + FInfoOrderBy_str + ') as ZYL_RowId FROM ' + FQueryTableName + ' where ' + AFilterStr + ' ' + FInfoGroupBy_str;
GetDataset(ASqlStr, ADQ_Count); //打开数据集
FDateCount := ADQ_Count.FieldByName('ZYL_RowId').AsInteger;
FPageCount := Trunc(FDateCount / FInfoPageSize_Int); //div
If FDateCount Mod FInfoPageSize_Int <> 0 Then
FPageCount := FPageCount + 1;
Label4.Caption := IntToStr(FDateCount);
Label6.Caption := IntToStr(FPageCount);
Edt_Page.Text := IntToStr(FInfoPageIndex_Int);
CLastPage.Enabled := FInfoPageIndex_Int <> 1;
CNextPage.Enabled := (FInfoPageIndex_Int <> FPageCount) And (FPageCount <> 0);
Except
End;
End;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBGridEhGrouping, ADODB, GridsEh, DBGridEh, DB, StdCtrls;
type
TForm1 = class(TForm)
Conn1: TADOConnection;
ds1: TDataSource;
ADOQuery1: TADOQuery;
dbgrdh1: TDBGridEh;
tbl1: TADOTable;
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn4: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
ADOQuery2: TADOQuery;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
SN:integer=500; //每页要显示的记录条数
N,ZYS:integer; //N显示页面累加,ZYS 总页数
ZHYY:Double; //最后一页
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
adoquery2.Close;
adoquery2.SQL.clear;
adoquery2.sql.add('select count(ID) as MYID from MJSJ');
adoquery2.Open;
ZYS:=ADOQuery2.FieldByName('MYID').AsInteger;
ZHYY:=Round(ZYS / SN);
end;
procedure TForm1.btn1Click(Sender: TObject); //第一页
begin
N:=0;
adoquery1.Close;
adoquery1.SQL.clear;
adoquery1.SQL.add('select top '+inttostr(SN)+' * from MJSJ');
adoquery1.Open;
end;
procedure TForm1.btn2Click(Sender: TObject); //下一页
begin
N:=N+SN;
adoquery1.Close;
adoquery1.SQL.clear;
adoquery1.SQL.Add('select top '+inttostr(SN)+' * from MJSJ where ID not in(select top '+inttostr(N)+' ID from MJSJ)');
adoquery1.Open;
end;
procedure TForm1.btn3Click(Sender: TObject); //上一页
begin
if (N-SN=0) then
begin
adoquery1.Close;
adoquery1.SQL.clear;
adoquery1.SQL.add('select top '+inttostr(SN)+' * from MJSJ');
adoquery1.Open;
end;
if (N-SN>0) then
begin
N:=N-SN;
adoquery1.Close;
adoquery1.SQL.clear;
adoquery1.SQL.Add('select top '+inttostr(SN)+' * from MJSJ where ID not in(select top '+inttostr(N)+' ID from MJSJ)');
adoquery1.Open;
end
ELSE Exit;
end;
procedure TForm1.btn4Click(Sender: TObject); //最后页
begin
adoquery1.Close;
adoquery1.SQL.clear;
adoquery1.SQL.Add('select top '+inttostr(SN)+' * from MJSJ where ID not in(select top '+inttostr(ZYS-SN)+' ID from MJSJ)');
adoquery1.Open;
end;
end.
Select * FROM (select * , ROW_NUMBER()Over(order by ID) as RowId
from Tabel1) as mytable
where RowId between 1 and 500