首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

难题求救,理不清思路了,高手看一下吧解决思路

2012-02-15 
难题求救,理不清思路了,高手看一下吧先不要简单的讲用动态SQL语句,前面一位大哥已经给我讲了,我也理解了,

难题求救,理不清思路了,高手看一下吧
先不要简单的讲用动态SQL语句,前面一位大哥已经给我讲了,我也理解了,但是仍然没有想出办法解决我的这个问题,请高手们仔细看一下下面:

有一个存储过程有3个输入参数,分别是@companname,@startdate,@enddate;代表的是公司名称,开始时间,结束时间,现在的要求是:
这3个参数各都有为空和不为空二种状态,组合起来就有N种结果,如果一条条写出来,应该比较麻烦,求高手看一下如何写出这种存储过程。

下面这个是全部不为空的写法
————————————————————————————————————
                        select   公司id,公司名称,sum(金额)   as   总额   from
                                    (
                                      select   *   from   1   where   请求时间   <   @enddate   and   请求时间   >   @startdate
                                      union   all
                                      select   *   from   2   where   请求时间   <   @enddate   and   请求时间   >   @startdate
                                    )   t   where   审批意见= '同意 '   and   公司名称=@companyname
                      group   by   公司id,公司名称   order   by   公司id
————————————————————————————————————


下面这个是全部为空的写法
————————————————————————————————————
                        select   公司id,公司名称,sum(金额)   as   总额   from
                                    (
                                      select   *   from   1                                      
                                      union   all
                                      select   *   from   2                                       )   t   where   审批意见= '同意 '                         group   by   公司id,公司名称   order   by   公司id
————————————————————————————————————

可能还需要注意的是要判断开始时间和结束时间是否正确,请高手帮个忙吧!


[解决办法]
where 请求时间 between @startdate and @enddate
[解决办法]
介绍一下用法:
这样用需要加小时段 如:2007-01-01 00:00:00 —2007-01-01 23:59:59
变量类型为datetime
set @startdate= '2007-01-01 00:00:00 '
set @enddate= '2007-01-01 23:59:59
where 请求时间 between @startdate and @enddate
变量类型为varchar
set @startdate = '20070101 '--取天为单位
set @enddate = '20070101 '
where convert(varchar(8),请求时间,112) between @startdate and @enddate


[解决办法]
create oric test
(
@companyname varchar(100)=null,
@enddate datetime=null,
@startdate datetime=null)
as
begin

select 公司id,公司名称,sum(金额) as 总额 from
(
select * from [1]
union all
select * from [2]
) t
where 审批意见= '同意 '
and 公司名称=(case when @companyname is null then 公司名称 else @companyname end)
and 请求时间 <(case when @enddate is null then 请求时间+1 else @enddate end)
and 请求时间> (case when @startdate is null then 请求时间-1 else @startdate end)
group by 公司id,公司名称 order by 公司id

end

这样不论你传几个变量,为空或不为空都无所谓了.

如只传一个参数:

exec test @companyname= '微软公司 '
[解决办法]
老兄,没有多少种组合
declare @sql varchar(8000)
declare @f varchar(400)
set @f=(case when @enddate is null then ' ' else '
where 请求时间 < ' ' '+@enddate+ ' ' ' ' end )+
(case when @startdate is null then ' ' else ' and 请求时间 > ' ' '+@startdate+ ' ' ' ' end)+ ' '
set @sql= 'select 公司id,公司名称,sum(金额) as 总额 from
(
select * from 1 '+@f+
' union all
select * from 2 '+@f+
') t where 审批意见= ' '同意 ' ' '+
(case when isnull(@corpname, ' ')= ' ' then ' ' else ' and 公司名称= ' ' '+@companyname+ ' ' ' ' end)+ '
group by 公司id,公司名称 order by 公司id '
exec(@sql)

热点排行