怎样在sql语句中使用变量。

lubaby726 2003-03-26 10:25:28
bmcode是一个变量
ADOQuery1.SQL.Add ( 'select * from bztab where 代码 like " bmcode %"') ;
对不对。正确的应怎样写
...全文
369 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
XXSingle 2003-03-26
  • 打赏
  • 举报
回复
up
5116888 2003-03-26
  • 打赏
  • 举报
回复
要在sql语句中使用参数前面加上":"冒号就可以了,然后给参数赋值,就像fansofamei(小天笑)说的.
zhyanger 2003-03-26
  • 打赏
  • 举报
回复
bmcode为string时:
ADOQuery1.SQL.Add ( 'select * from bztab where 代码 like '+ ''''+bmcode+ '%'+'''')
bmcode为integer时:
ADOQuery1.SQL.Add ( 'select * from bztab where 代码 like '+ inttostr(bmcode)+ '%')
snowfield 2003-03-26
  • 打赏
  • 举报
回复
Sorry,打错了。假定你的变量是字符串型的
snowfield 2003-03-26
  • 打赏
  • 举报
回复
就是用字符串连接运算,假定你的变量是字符型的,否则自己转。
ADOQuery1.SQL.Add ( 'select * from bztab where 代码 like '+bmcode+'%') ;
fansofamei 2003-03-26
  • 打赏
  • 举报
回复
看看delphi自己的例子,就是这样写的,没事多按按F1有好处
Query2.SQL.Clear;

Query2.SQL.Add('INSERT INTO COUNTRY (NAME, CAPITAL, POPULATION)');
Query2.SQL.Add('VALUES (:Name, :Capital, :Population)');

Query2.Params[0].AsString := 'Lichtenstein';
Query2.Params[1].AsString := 'Vaduz';
Query2.Params[2].AsInteger := 420000;
Query2.ExecSQL;
lubaby726 2003-03-26
  • 打赏
  • 举报
回复
ADOQuery1.SQL.Add('select * from bztab where 代码 like ''%bmcode%'' ');
lubaby726 2003-03-26
  • 打赏
  • 举报
回复
我已解决
lubaby726 2003-03-26
  • 打赏
  • 举报
回复
ADOQuery1.SQL.Add('select * from bztab where 代码 like :aaa');
这样写也不对呀

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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