请问在java 里调用存储过程,显示数据,当传的参数为NULL时,查询所有数据,这个怎处理呢?create procedure ProcInventory 
@GoodsCode nvarchar(50),
@GoodName nvarchar(50),
@BeginDate  nvarchar(50),
@EndDate  nvarchar(50)
as
declare @sql varchar(4000)create table #test(ord varchar(30),GoodsCode nvarchar(20),GoodsName nvarchar(50),Amount nvarchar(50),UnitPrice nvarchar(50),
SumMoney nvarchar(10),Brand nvarchar(50),Unit nvarchar(50))
set @sql= 'insert into #Test(ord ,GoodsCode,GoodsName ,Amount ,UnitPrice ,
SumMoney ,Brand ,Unit )
select ROW_NUMBER() over(order by TbShGoods.GoodsCode) as ord,  TbShGoods.GoodsCode,i.GoodName,(i.Amount-isnull(0,o.Amount)) as Amount,i.UnitPrice
    ,(i.Sum-isnull(0,o.Sum)) as Sum ,Brand,Unit
     from tbshindetail i left join tbshoutdetail o on i.GoodsID=o.GoodsID
     inner join TbShin on i.InID=TbShin.InID
         inner join TbShGoods on i.GoodsID=TbShGoods.GoodsID
where   i.GoodName='''+@GoodName+''' and TbShin.EnterDate between  '''+@BeginDate+'''
 and '''+@EndDate +''' and TbShGoods.GoodsCode='''+@GoodsCode+''''
print '11'exec(@sql)
print @sql

解决方案 »

  1.   

    楼主的意思是传入的参数为空或者为null或者有值,都能写在一条命令里面而不会出错的实现?
      

  2.   

    就是传入的参数为空或者为null或者有值 都可以执行存储过程,并且参数为空或者为null就查询所有数据相当于不要条件查询。就像页面的查询功能一样,当在输入框里不填数据时,点击查询,就把所有数据查询出来,当填入值,就按照这个值查询。
      

  3.   


    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ProcInventory]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[ProcInventory]
    go
    create procedure ProcInventory 
    @GoodsCode nvarchar(50),
    @GoodName nvarchar(50),
    @BeginDate  nvarchar(50),
    @EndDate  nvarchar(50)
    as
    begin
    declare @sql varchar(4000)create table #test(ord varchar(30),GoodsCode nvarchar(20),GoodsName nvarchar(50),Amount nvarchar(50),UnitPrice nvarchar(50),
    SumMoney nvarchar(10),Brand nvarchar(50),Unit nvarchar(50))
    /*这里你也可以根据情况自己定义条件 最好不要直接null,在程序里面判断null 赋个特殊值进来做判断,或者在存储过程里面做判断也行。比如if(@GoodsCode is null) set @GoodsCode = '0' 然后在后面直接判断是否为'0',做相应处理。 */
    if (@GoodsCode is null and @GoodName is null and @BeginDate is null and @EndDate is null) 
    begin
    set @sql = 'select 1' --这里改成你要的查询语句
    end
    else
    begin
    set @sql= 'insert into #Test(ord ,GoodsCode,GoodsName ,Amount ,UnitPrice ,
    SumMoney ,Brand ,Unit )
    select ROW_NUMBER() over(order by TbShGoods.GoodsCode) as ord,  TbShGoods.GoodsCode,i.GoodName,(i.Amount-isnull(0,o.Amount)) as Amount,i.UnitPrice
        ,(i.Sum-isnull(0,o.Sum)) as Sum ,Brand,Unit
         from tbshindetail i left join tbshoutdetail o on i.GoodsID=o.GoodsID
         inner join TbShin on i.InID=TbShin.InID
             inner join TbShGoods on i.GoodsID=TbShGoods.GoodsID
    where   i.GoodName='''+@GoodName+''' and TbShin.EnterDate between  '''+@BeginDate+'''
     and '''+@EndDate +''' and TbShGoods.GoodsCode='''+@GoodsCode+''''
    print '11'
    end--exec(@sql)
    print @sql
    end
    go
    exec ProcInventory 1,1,1,1
    go
    exec ProcInventory null,null,null,null
    /*
    11
    insert into #Test(ord ,GoodsCode,GoodsName ,Amount ,UnitPrice ,
    SumMoney ,Brand ,Unit )
    select ROW_NUMBER() over(order by TbShGoods.GoodsCode) as ord,  TbShGoods.GoodsCode,i.GoodName,(i.Amount-isnull(0,o.Amount)) as Amount,i.UnitPrice
        ,(i.Sum-isnull(0,o.Sum)) as Sum ,Brand,Unit
         from tbshindetail i left join tbshoutdetail o on i.GoodsID=o.GoodsID
         inner join TbShin on i.InID=TbShin.InID
             inner join TbShGoods on i.GoodsID=TbShGoods.GoodsID
    where   i.GoodName='1' and TbShin.EnterDate between  '1'
     and '1' and TbShGoods.GoodsCode='1'
    select 1
    */
      

  4.   

    如果是用的hibernate反转生成的dao,那么里面是封装好了的,对你这类情况可以进行处理;如果是自己操作数据库的话,我以前做了一下,是用的mysql做的,你可以看一下:
      

  5.   

    不好意思,都不知道“引用”这个功能,我是用hibernate的,刚才试过一下,如果全部为NULL就可以了。我的意思是,比如有@GoodsCode,@GoodName,@BeginDate,@EndDate,这四个参数,当时其中参数值为NULL时就不查询这个条件。相当于@GoodsCode,@GoodName这两个参数为空时,就只把@BeginDate,@EndDate作为条件查询,不知道这样能不能看清楚我写的意思呢?
      

  6.   

    不好意思,都不知道“引用”这个功能,我是用hibernate的,刚才试过一下,如果全部为NULL就可以了。我的意思是,比如有@GoodsCode,@GoodName,@BeginDate,@EndDate,这四个参数,当时其中参数值为NULL时就不查询这个条件。相当于@GoodsCode,@GoodName这两个参数为空时,就只把@BeginDate,@EndDate作为条件查询,不知道这样能不能看清楚我写的意思呢?我上面的例子就是这个意思!set_out_time为空,或者为null都可以进行查询,不过这是mysql里面的用法,其它数据库也应该是类似的!
      

  7.   

    我是在SSH里做一个报表的是用存储过程来查询数据,然后参数作为查询的条件。这个在代码里不知道怎判断,所以只能在存储过程里判断了
      

  8.   

    不好意思,都不知道“引用”这个功能,我是用hibernate的,刚才试过一下,如果全部为NULL就可以了。我的意思是,比如有@GoodsCode,@GoodName,@BeginDate,@EndDate,这四个参数,当时其中参数值为NULL时就不查询这个条件。相当于@GoodsCode,@GoodName这两个参数为空时,就只把@BeginDate,@EndDate作为条件查询,不知道这样能不能看清楚我写的意思呢?我上面的例子就是这个意思!set_out_time为空,或者为null都可以进行查询,不过这是mysql里面的用法,其它数据库也应该是类似的!呵呵,不行,是全部为NULL才能查数据,如果有一个参数有值就查不出数据了
      

  9.   

    不好意思,都不知道“引用”这个功能,我是用hibernate的,刚才试过一下,如果全部为NULL就可以了。我的意思是,比如有@GoodsCode,@GoodName,@BeginDate,@EndDate,这四个参数,当时其中参数值为NULL时就不查询这个条件。相当于@GoodsCode,@GoodName这两个参数为空时,就只把@BeginDate,@EndDate作为条件查询,不知道这样能不能看清楚我写的意思呢?我上面的例子就是这个意思!set_out_time为空,或者为null都可以进行查询,不过这是mysql里面的用法,其它数据库也应该是类似的!呵呵,不行,是全部为NULL才能查数据,如果有一个参数有值就查不出数据了小括号后面用and连接就行了,看图吧:可能我上面IFNULL表达式忘记与一个数做判断了,这个数是自己设定的,意思是能使表达式成立
      

  10.   

    like case when N'' =isnull(@userType,N'') then '%' else @userType end