chenhuajuan 2013-11-14 19:47
浏览 595
已采纳

请问在java 里调用存储过程,显示数据,当传的参数为NULL时,查询所有数据,这个怎处理呢?

请问在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条回答 默认 最新

  • iteye_7589 2013-11-17 11:56
    关注

    未测试,大概思路是定义一个where变量
    然后依次检查参数变量,如果参数非null,则where 加上比较 表达式。

    每次检查一下是否需要加上and运算符。

    [code="java"]
    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 '
    set @wheres = ''
    if @GoodName is not null then
    @where = @where +'''+@GoodName+'''
    endif

    if @GoodName is not null then
    if @where != '' then
    @where = @where +' and '
    endif
    @where = @where +'''+@GoodName+'''
    endif
    .....

    if @where != '' then
    @where = ' where '+@where
    endif

    exec(@sql+@where)
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog