你的意思就是动态组织SQL语句,是吧?
给你一个比较简单点的例子,看看。
这是三层结构中,通过ClientDataSet改写服务端的SQL语句中的程序。
procedure TRe_Ghdwwl_frm.BitBtn1Click(Sender: TObject);
var
n,m:Integer;
sql,sql1,s_com,s_item:wideString;
dat1,dat2:string;
begin
if sbStrarDate.Down then
dat1:=DateTimeToStr(dtpStrarDate.dateTime)
else
dat1:='1980-01-01';
if sbLastDate.Down then
dat2:=DateTimeToStr(dtpLastDate.dateTime)
else
dat2:='2100-01-01';
m:=lvComList.Items.Count;
if spComList.Down and (m>0) then
begin
s_com:=' and comdcode in ( ';
for n:=1 to mdo
begin
if m=n then
s_com:=s_com+''''+lvComList.Items[n-1].Caption+''''+')'
else
s_com:=s_com+''''+lvComList.Items[n-1].Caption+''',';
end;
end;
m:=lvItemList.Items.Count;
if spItemList.Down and (m>0) then
begin
s_item:=' and itemdcode in ( ';
for n:=1 to mdo
begin
if m=n then
s_item:=s_item+''''+lvItemList.Items[n-1].Caption+''''+')'
else
s_item:=s_item+''''+lvItemList.Items[n-1].Caption+''',';
end;
end;
sql:=' Select comdcode,comname,Sum(amtpaid) as a,0 as b,0 as c,0 as d '
+' from t_salelist Where optype=''购入'' '+s_com+s_item+' and whflag>=''2'' and saleflag>=''2'' and configdate<=#'+dat1+'# '
+' GROUP BY comdcode,comname '
+' union '
+' Select comdcode,comname,0 as a,Sum(tamt) as b,0 as c,0 as d '
+' from t_salelist Where optype=''购入'' '+s_com+s_item+' and whflag>=''2'' and saleflag>=''2'' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# '
+' GROUP BY comdcode,comname '
+' union '
+' Select comdcode,comname,0 as a,0 as b,Sum(tamt-amtpaid) as c,0 as d '
+' from t_salelist Where optype=''购入'' '+s_com+s_item+' and whflag>=''2'' and saleflag>=''2'' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# '
+' GROUP BY comdcode,comname '
+' union '
+' Select comdcode,comname,0 as a,0 as b,0 as c,Sum(amtpaid) as d '
+' from t_salelist Where optype=''购入'' '+s_com+s_item+' and whflag>=''2'' and saleflag>=''2'' and configdate<=#'+dat2+'# '
+' GROUP BY comdcode,comname ';
sql1:=s_item+' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# ';
if cdsGhwl_a.Active then
cdsGhwl_a.Close;
if cdsGhwl.Active then
cdsGhwl.Close;
cdsGhwl.DataRequest(sql);
cdsGhwl_b.DataRequest(sql1);
cdsGhwl.Open;
pnlFind.Visible:=False;
end;