delphi中写sql语句真烦人,请帮忙修改一个sql,谢谢了!!!

山水无言 2004-08-11 10:40:15
我的form上有一个TRadioGroup(其中设置6个单选项)、两个TDateTimePicker、一个TButton、一个TDBgrid、一个TDataSource、一个TADOquery和一个TADOconnection。过程代码如下:
  现在,在sql语局上出现了下列麻烦:
  (1) 按照下面的语句运行,系统提示:应该日期的地方出现了字符串,下面的代码正确的应该怎么写?
(2) 原来,这一句:from table1 a,table2 b where a.zd2=b.zd2 and substr(b.zd2,1,2)=''TT'' and b.leixin_id=1是准备用like关键字写的:
'from table1 a,table2 b where a.zd2=b.zd2 and b.zd2,1,2 like 'TT%' and b.leixin_id=1 可是试了半天,就是没办法写出正确的语句,请问,如果用like关键字来写,应该怎么写?
不好意思,应该是很简单的问题,在此麻烦大家了。:)

procedure TForm11.Button1Click(Sender: TObject);
begin
with adoquery1 do
begin
close;
sql.Clear;
if radiogroup1.itemindex=0 then begin
sql.Add('select a.zd1,b.zd2,b.zd_date,b.beizhu ');
sql.Add('from table1 a,table2 b where a.zd2=b.zd2 and substr(b.zd2,1,2)=''TT'' and b.leixin_id=1 ');
sql.Add('and b.zd_date>=(to_date(:y_begin ,''yyyymmdd'')-1) and b.zd_date<=(to_date(:y_end,''yyyymmdd''))');
sql.Add('and b.STATE>0 and substr(a.zd1,1,2) in (''11'',''12'',''13'',''14'') ');
sql.Add('AND B.fwlx_id=1 and a.pppid not in (4,5,6) order by b.zd2');
parameters.ParamByName('y_begin').value:=t_begin.Date;
parameters.ParamByName('y_end').value:=t_end.date;
open;
end;
ShowMessage('搜索完毕!!!');
end;
end;
...全文
320 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzxyd 2004-08-13
  • 打赏
  • 举报
回复
气死我啦,这么简单的东西,搞了两天都没有结果
oracle表里b.zd_date是date型的

sql.Add('AND (b.zd_date BETWEEN :y_begin AND :y_end) ')
......
parameters.ParamByName('y_begin').value:=t_begin.Date;
parameters.ParamByName('y_end').value:=t_end.Date;
上午,我用了上面的方法,可是就是提示错误
请帮帮忙呀
----------------------------------
回复:
可改为
sql.add('and (b.zd_date between '':y_begin'' and '':y_end''')
.......
parameters.ParamByName('y_begin').value:=FormatDatetime('yyyy-mm-dd hh:mm:ss',t_begin.date);
parameters.ParamByName('y_end').value:=FormatDateTime('yyyy-mm-dd hh:mm:ss',t_end.date);
ForumExplorer 2004-08-13
  • 打赏
  • 举报
回复
UP~~~~~~~~
----------------------
《CSDN论坛新助手 CSDN's forum Explorer》
1、更快速的浏览
2、更方便地保存
3、更快捷的收/发短信
下载地址:http://www.seeyou.com.cn/CoolSlob/CSDNExplorer.exe
更多说明:http://community.csdn.net/Expert/TopicView.asp?id=3255966
山水无言 2004-08-13
  • 打赏
  • 举报
回复
呵呵,结帐不急,但是我不会忘记给我帮助的人.
我还需要找到下面的答案呢,
............
sql.Add('and b.zd_date between :y_begin and :y_end ');
.............
parameters.ParamByName('y_begin').value:=formatdatetime('yyyy/mm/dd',t_begin.Date);
parameters.ParamByName('y_end').value:=formatdatetime('yyyy/mm/dd',t_end.Date+1);
.............
通过了,很高兴呀!
可是上述语句为什么能通过,我不明白.请帮我解释解释吧!
我不明白的地方在于:
y_begin和y_end变量是string型的,而oracle数据库表的dz_date字段是date型的,为什么
sql.Add('and b.zd_date between :y_begin and :y_end ');能通过?
cctv6012cn 2004-08-13
  • 打赏
  • 举报
回复
呵呵。通过了啊。那要恭喜楼主罗。记得要结帖啊
山水无言 2004-08-13
  • 打赏
  • 举报
回复
............
sql.Add('and b.zd_date between :y_begin and :y_end ');
.............
parameters.ParamByName('y_begin').value:=formatdatetime('yyyy/mm/dd',t_begin.Date);
parameters.ParamByName('y_end').value:=formatdatetime('yyyy/mm/dd',t_end.Date+1);
.............
通过了,很高兴呀!
可是上述语句为什么能通过,我不明白.请帮我解释解释吧!
我不明白的地方在于:
y_begin和y_end变量是string型的,而oracle数据库表的dz_date字段是date型的,为什么
sql.Add('and b.zd_date between :y_begin and :y_end ');能通过?
BigAngell 2004-08-12
  • 打赏
  • 举报
回复
关注...
whisht 2004-08-12
  • 打赏
  • 举报
回复
回复人: chinaandys(风滴泪,雨含笑) ( ) 信誉:97 2004-08-12 09:47:00 得分: 0



lanpa007 2004-08-12
  • 打赏
  • 举报
回复
sql 语句:
and b.zd_date
between to_date('2003-08','yyyy-mm') and to_date('2004-08','yyyy-mm')
自己在delphi 里面整理一下吧
Sant11 2004-08-12
  • 打赏
  • 举报
回复
用format格式话
formatdatetime('yyyymmdd',datetimepicker1);
记住在写的时候用#39代替''就好的多.在变量的地方.
山水无言 2004-08-12
  • 打赏
  • 举报
回复
我要求年月日的格式是:YYYYMMDD
我第一次使用Tdatetimepicker控件,怎么这么麻烦?请帮忙我的sql该怎么写,
山水无言 2004-08-12
  • 打赏
  • 举报
回复
气死我啦,这么简单的东西,搞了两天都没有结果
oracle表里b.zd_date是date型的

sql.Add('AND (b.zd_date BETWEEN :y_begin AND :y_end) ')
......
parameters.ParamByName('y_begin').value:=t_begin.Date;
parameters.ParamByName('y_end').value:=t_end.Date;
上午,我用了上面的方法,可是就是提示错误
请帮帮忙呀
山水无言 2004-08-12
  • 打赏
  • 举报
回复
请继续呀!
chinaandys 2004-08-12
  • 打赏
  • 举报
回复
帮您顶。其实我爱用sql server
cronuz 2004-08-12
  • 打赏
  • 举报
回复
sql.Add('AND (b.zd_date BETWEEN :y_begin AND :y_end) ')
{...}
parameters.ParamByName('y_begin').value:=t_begin.Date;

(注:如果「b.zd_date」是DATE型、直接用TDateTimePicker赋给参数)
cronuz 2004-08-12
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var str: String;
begin
if radiogroup1.itemindex<>0 then exit;

with adoquery1 do
begin
str := ' select a.zd1,b.zd2,b.zd_date,b.beizhu ';
str := str + ' from table1 a, table2 b ';
str := str + ' where (a.zd2=b.zd2) and (b.zd2 like ''TT%'') and (b.leixin_id=1) ';
str := str + ' and (b.zd_date BETWEEN to_date(:y_begin,''yyyymmdd'') AND to_date(:y_end,''yyyymmdd'')) ';
str := str + ' and (b.STATE>0) and (substr(a.zd1,1,2) in (''11'',''12'',''13'',''14'')) ';
str := str + ' and (b.fwlx_id=1) and (a.pppid not in (4,5,6)) ';
str := str +' order by b.zd2 ';
showMessage(str); //確認SQL語句的内容

Close;
Parameters.Clear;
Parameters.AddParameter.Name := 'y_begin';
Parameters.AddParameter.Name := 'y_end';
SQL.Clear;
SQL.Text := str;
Parameters.ParamByName('y_begin').Value := DateToStr(DateTimePicker1.Date);
Parameters.ParamByName('y_end').Value := DateToStr(DateTimePicker2.Date);
Open;
end;

ShowMessage('搜索完?!!!');
end;

(注:DateTimePicker1.Format := 'yyyyMMdd'; 幇NI写了写、試試看)
山水无言 2004-08-11
  • 打赏
  • 举报
回复
谢谢各位帮忙。
qybao(阿宝)好,
sql.Add('and b.zd_date>=(to_date(:y_begin ,''yyyymmdd'')-1) and b.zd_date<=(to_date(:y_end,''yyyymmdd''))');
parameters.ParamByName('y_begin').value:=t_begin.Date;
请给我写一个正确答案,谢谢!我一个晚上被这个sql搞的头晕了。
longtusoft 2004-08-11
  • 打赏
  • 举报
回复
帮顶
cronuz 2004-08-11
  • 打赏
  • 举报
回复
sql.Add('select a.zd1,b.zd2,b.zd_date,b.beizhu ');
sql.Add('from table1 a,table2 b where a.zd2=b.zd2 and b.zd2 like :zd2_var and b.leixin_id=1 ');
{...}
parameters.ParamByName('zd2_var').value := Edit1.Text+'%';

(注:把like也写成参数形式「b.zd2 like :zd2_var 」、可灵活负值)
qybao 2004-08-11
  • 打赏
  • 举报
回复
sql.Add('and b.zd_date>=(to_date(:y_begin ,''yyyymmdd'')-1) and b.zd_date<=(to_date(:y_end,''yyyymmdd''))');
parameters.ParamByName('y_begin').value:=t_begin.Date;
这两句没看出问题吗
to_date是把字符串转成日期的,而你程序外边给参数赋值赋的也是日期类型,所以就不对了
要么修改你的sql文不用to_date,要么把你程序外边取到的日期转成字符串再赋给参数。
qybao 2004-08-11
  • 打赏
  • 举报
回复
sql.Add('from table1 a,table2 b where a.zd2=b.zd2 and substr(b.zd2,1,2) like ''TT%'' and b.leixin_id=1 ');
加载更多回复(1)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧