delcare @a int
select * from table1 where a=@a or @a=0
如果@=0全部信息,否者为需要的信息
建过程

解决方案 »

  1.   

    谢谢caiyunxia(monkey) ,你的意思是根据输入的@a做查询的条件,但是不能知道a的值,数据表是动态填充的,a的值是根据别的表传过来的
    请指教
      

  2.   

    select @a= from table2 where ...
    select * from table1 where a=@a or @a=0
      

  3.   

    declare cursor cur_1 for
    select distinct a from table1;
    open cur_1;
    fetch cur_1 into :a1;
    do while sqlca.sqlcode = 0 then
       select * from table1 where a = a1;
    可以定义一个游标,首先取出所有存在的a值,然后根据取出的a值去取得数据。
      

  4.   

    你的办法是可行,但那样我就要不停的操作数据库,对程序的健壮性有影响,我还想对分组的数据进行操作
    有没有仅从table1就可以实现的办法
      

  5.   

    create proc test
      @Num int
    as
    if exists (select 1 from tempdb..sysobjects where name='##TEMP')
       select IDENTITY(int,1,1) as id,a 
       into ##Temp
       from (
         select distinct a from table1   ) as xselect x.* from table1 x,##Temp t
    where x.a=t.a
    and t.id=@Numgo调用:
    declare @Num int
    declare @Cnt int
    select @cnt=count(distinct a) from table1
    set @Num=1
    while @Num<=@Cnt
    begin 
       exec test @Num
       set @Num=@Num+1
    end
      

  6.   

    调用:
    declare @Num int
    declare @Cnt int
    select @cnt=count(distinct a) from table1
    set @Num=1
    while @Num<=@Cnt
    begin 
       exec test @Num
       set @Num=@Num+1
    enddrop table ##Temp