大家好! 
  我的数据表table里有姓名:string,年龄:int等字段,想查询年龄为某一个数的所有人的姓名和年龄;
 select * from table where 年龄=age,age为一个变量,现在问题是我直接用变量age查询出错。
正确的语句该怎么写,请大家帮忙.

解决方案 »

  1.   

    declare @age int
    set @age=20select * from table where 年龄=@age
      

  2.   


    create table test(age int)
    insert into test 
    select 24 union all
    select 23 union all
    select 22
    godeclare @age int
    set @age=22select * from test where age=@ageage         
    ----------- 
    22(所影响的行数为 1 行)
      

  3.   

    declare @age int ---sql申明变量的方法
      

  4.   

    我的查询语句是在delphi 中写的,不是通过 存储过程
      

  5.   

    select * from table where 年龄=''+age+''
      

  6.   


    strSql="select * from table where 年龄="+age类似这样拼接字符串的形式。
    不过,最好还是用参数写SQL,这样比较好。
      

  7.   

    ADOQuery.SQL.Text := 'select * from table where 年龄=' + age + ';
    這樣就可以了