请问怎么在sql语句中使用变量?
如:
sql.add('select 商品一级分类ID from 商品一级分类 where 商品一级分类名=ComboBox2.text');
ComboBox2.text为要使用的变量

解决方案 »

  1.   

    这样写是对的,为什么
    'Select 商品一级分类ID from YourTableName Where 商品一级分类名 = '''+combobox2.text+''''
    后面怎么那么多',都代表什么意思
      

  2.   

    没有这么多的'''',你的sql语句就是错误的。
    'Select 商品一级分类ID from YourTableName Where 商品一级分类名 ='+combobox2.text
    生成的sql语句是这样的:Select 商品一级分类ID from YourTableName Where 商品一级分类名 =123'Select 商品一级分类ID from YourTableName Where 商品一级分类名 = '''+combobox2.text+''''
    生成的sql语句是这样的:Select 商品一级分类ID from YourTableName Where 商品一级分类名 ='123'''''就是字符' 相当于c++中的"c:\\aa.txt",为什么要两个斜杠呢,你学过C++吧
      

  3.   

    sqlstr:='select 商品一级分类ID from 商品一级分类 where 商品一级分类名="' + ComboBox2.text + '"';with sql do
      begin
        close;
        sql.clear;
        sql.add(sqlstr);
        open;
      end;这样写是不是看得清晰些?
      

  4.   

    Query.sql.add('select 商品一级分类ID from 商品一级分类 where 商品一级分类名=:text');
    Query.Parameters.ParaByName('text').Value:= ComboBox2.text;