我想通过RadioGroup1来切换‘在院’和‘出院’,DbGrid1连接Dm.ADOQuery_BedInfo, 我想通过在原有的SQL语句后添加限制条件:
‘ and (OutDay is null)’或‘ and (OutDay is not null)’来达到效果,
但结果在语句中间部分有‘#$D#$A’,所以数据集返回是空,怎么去掉呢?
源码如下:procedure TFrm_ZhuYuanBu.RadioGroup1Click(Sender: TObject);
var
s: string;
begin
Case RadioGroup1.ItemIndex of
0: //在院
begin
Dm.ADOQuery_BedInfo.Close;
s := Dm.ADOQuery_BedInfo.SQL.Text;
Dm.ADOQuery_BedInfo.SQL.Text := Dm.ADOQuery_BedInfo.SQL.Text + ' and (OutDay is null) ';
Dm.ADOQuery_BedInfo.Prepared;
Dm.ADOQuery_BedInfo.ExecSQL;
Dm.ADOQuery_BedInfo.Open;
Dm.ADOQuery_BedInfo.SQL.Text := s;
end;
1: //出院
begin
Dm.ADOQuery_BedInfo.Close;
s := Dm.ADOQuery_BedInfo.SQL.Text;
Dm.ADOQuery_BedInfo.SQL.Text := Dm.ADOQuery_BedInfo.SQL.Text + ' and (OutDay is not null) ';
Dm.ADOQuery_BedInfo.Prepared;
Dm.ADOQuery_BedInfo.ExecSQL;
Dm.ADOQuery_BedInfo.Open;
Dm.ADOQuery_BedInfo.SQL.Text := s;
end;
end;
end;
‘ and (OutDay is null)’或‘ and (OutDay is not null)’来达到效果,
但结果在语句中间部分有‘#$D#$A’,所以数据集返回是空,怎么去掉呢?
源码如下:procedure TFrm_ZhuYuanBu.RadioGroup1Click(Sender: TObject);
var
s: string;
begin
Case RadioGroup1.ItemIndex of
0: //在院
begin
Dm.ADOQuery_BedInfo.Close;
s := Dm.ADOQuery_BedInfo.SQL.Text;
Dm.ADOQuery_BedInfo.SQL.Text := Dm.ADOQuery_BedInfo.SQL.Text + ' and (OutDay is null) ';
Dm.ADOQuery_BedInfo.Prepared;
Dm.ADOQuery_BedInfo.ExecSQL;
Dm.ADOQuery_BedInfo.Open;
Dm.ADOQuery_BedInfo.SQL.Text := s;
end;
1: //出院
begin
Dm.ADOQuery_BedInfo.Close;
s := Dm.ADOQuery_BedInfo.SQL.Text;
Dm.ADOQuery_BedInfo.SQL.Text := Dm.ADOQuery_BedInfo.SQL.Text + ' and (OutDay is not null) ';
Dm.ADOQuery_BedInfo.Prepared;
Dm.ADOQuery_BedInfo.ExecSQL;
Dm.ADOQuery_BedInfo.Open;
Dm.ADOQuery_BedInfo.SQL.Text := s;
end;
end;
end;
你可以设置断点看看将S设置为全局变量,并且在formshow的时候赋值,就是不带任何其他where查询条件的sql,形如:select * from table where 1=1 procedure TFrm_ZhuYuanBu.RadioGroup1Click(Sender: TObject);
begin
Case RadioGroup1.ItemIndex of
0: //在院
begin
Dm.ADOQuery_BedInfo.Close;
Dm.ADOQuery_BedInfo.SQL.Text := S+ ' and (OutDay is null) ';
Dm.ADOQuery_BedInfo.Open;
end;
1: //出院
begin
Dm.ADOQuery_BedInfo.Close;
s := Dm.ADOQuery_BedInfo.SQL.Text;
Dm.ADOQuery_BedInfo.SQL.Text := S+ ' and (OutDay is not null) ';
Dm.ADOQuery_BedInfo.Open;
end;
end;
end;
在使用adoquery.sql.add時請請先用adoquery.sql.clear清空sql語句;
你可以再用showmessage(adoquery1.sql.text)來看一下sql語句有沒有錯錯在哪裡.
Dm.ADOQuery_BedInfo.SQL.Text := s;去掉就可以了,但我之所以要引入‘s’,是起一个临时变量的作用,因为我想通过在原有的SQL语句后添加限制条件,故我要保存原有的SQL语句,并且复原它,怎么办?