输入:@productid功能:1。 当@productid =0时 返回所有记录
      2。 当@productid !=0时 返回产品号=@productid  的纪录想写成一条SQL语句。

解决方案 »

  1.   

    alter procedure sp_test(@productid int)
    as
    declare @table table
    (
    id int identity(1,1) not null,
    productid  int not null
    )insert into @table select 1
    insert into @table select 2
    insert into @table select 3
    insert into @table select 4
    insert into @table select 5
    insert into @table select 6select *
    from @table a
    where productId = (case when @productid != 0 then @productid else null end)
    union
    select *
    from @table
    where @productid = 0exec sp_test 2直接想到的就这样了大概
      

  2.   

    忘了,测试的时候还要一个productid为0的记录
      

  3.   

    谢谢了,其实没有为0的纪录的,这样可以用null来获得全部的纪录,我就是没想到。