select * from tb 结果如下:ID Title
1  AA
2  BB现在通过 sql 动态增加了一列ID Title Per
1  AA   2
2  BB   3现在需要 select * from tb where Per = 2 , 则提示列名 Per 无效, 请问这种情况,该如何处理呢?

解决方案 »

  1.   

    select *
    from (select ID,Title,Per=动态生成的列) t
    where Per = 2
      

  2.   

    修正并加测试数据一试---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[Title] varchar(2))
    insert [tb]
    select 1,'AA' union all
    select 2,'BB'
     
    ---查询---
    select *
    from (select ID,Title,Per=id+1 from tb) t
    where Per = 2---结果---
    ID          Title Per         
    ----------- ----- ----------- 
    1           AA    2(所影响的行数为 1 行)
      

  3.   

    这种要用存储过程才好写一些,一句sql语句看来是稿不定的了
    存储过程可以传入动态参数列名和参数列值,然后在里面exec(sql)语句,结果就出来了;
      

  4.   


    select * from (动态生成的子查询) t where per = 2
      

  5.   

    列增加了就得从新抽取数据的。
    select *
    from (select ID,Title,Per=动态生成的列 from .....) t
    where Per = 2