where 请求时间 between  @startdate and  @enddate

解决方案 »

  1.   

    介绍一下用法:
    这样用需要加小时段 如: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
      

  2.   

    create oric test
    (
    @companyname varchar(100)=null,
    @enddate datetime=null,
    @startdate datetime=null)
    as
    beginselect 公司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 公司idend这样不论你传几个变量,为空或不为空都无所谓了.如只传一个参数:exec test @companyname='微软公司'
      

  3.   

    create oric test--->create proc test
      

  4.   

    老兄,没有多少种组合
    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)
      

  5.   

    有个疑问,不太懂,那就是:
    select 公司id,公司名称,sum(金额) as 总额 from 
    (
                       select * from [1]
                       union all
                       select * from [2]
    ) t 
    中并没有相关于"请求时间"的概念,这个时侯把
    and 请求时间<(case when @enddate is null then 请求时间+1 else @enddate end)
    and 请求时间>(case when @startdate is null then 请求时间-1 else @startdate end
    加到这个t表中能够行得通么,小弟还不是很了解
      

  6.   

    有个疑问,不太懂,那就是:
    select 公司id,公司名称,sum(金额) as 总额 from 
    (
                       select * from [1]
                       union all
                       select * from [2]
    ) t 
    中并没有相关于"请求时间"的概念,这个时侯把
    and 请求时间<(case when @enddate is null then 请求时间+1 else @enddate end)
    and 请求时间>(case when @startdate is null then 请求时间-1 else @startdate end
    加到这个t表中能够行得通么,小弟还不是很了解
      

  7.   

    有个疑问,不太懂,那就是:
    select 公司id,公司名称,sum(金额) as 总额 from 
    (
                       select * from [1]
                       union all
                       select * from [2]
    ) t 
    中并没有相关于"请求时间"的概念,这个时侯把
    and 请求时间<(case when @enddate is null then 请求时间+1 else @enddate end)
    and 请求时间>(case when @startdate is null then 请求时间-1 else @startdate end
    加到这个t表中能够行得通么,小弟还不是很了解