例如:我写了一个存储过程有两参数如何不输入开始日期和结束日期就就查出所有记录
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GOALTER  proc proc_bosChangeItem(
@beginDate datetime,
@endDate datetime

)
as
set nocount on
select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
left join t_boschangeitem b on a.fid=b.fid
left join t_department c on b.fdeptid=c.fitemid 
where b.fchecker>0 and b.fdate between @beginDate and @endDate
group by c.fname
order by 换货金额 desc 
select * from #temp
drop table #temp
GO

解决方案 »

  1.   


    where b.fchecker>0 and b.fdate between isnull(@beginDate,0) and isnull(@endDate ,'2050-1-1')
      

  2.   

    b.fdate between isnull(@beginDate,b.fdate) and isnull(@endDate,b.fdate)
      

  3.   

    SET QUOTED_IDENTIFIER ON 
    GO 
    SET ANSI_NULLS ON 
    GO ALTER  proc proc_bosChangeItem( 
    @beginDate datetime =NULL, 
    @endDate datetime =NULL) 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between isnull(@beginDate,0) and isnull(@endDate,'9999-12-31') 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 
    --调用 
    exec proc_bosChangeItem
      

  4.   

    6楼的试了吗? 注意参数中的=NULL
      

  5.   

    SET QUOTED_IDENTIFIER ON 
    GO 
    SET ANSI_NULLS ON 
    GO ALTER  proc proc_bosChangeItem( 
    @beginDate datetime ='1900-01-01', 
    @endDate datetime ='9999-12-31') 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 要不这样吧,直截了当。
      

  6.   

    create  proc proc_bosChangeItem( 
    @beginDate datetime ='1900-01-01', 
    @endDate datetime ='9999-12-31') 
    as 
    set nocount on 
    select * from sysobjects where crdate between @beginDate and @endDate
    GO exec proc_bosChangeItem -->测试成功
      

  7.   

    ALTER  proc proc_bosChangeItem( 
    @beginDate datetime, 
    @endDate datetime 

    as 
    set nocount on select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 
    into #temp 
    from t_boschangeitementry a left join t_boschangeitem b on a.fid=b.fid 
     left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 
    and b.fdate between @beginDate and @endDate 
    or b.fchecker>0
    and @BeginDate = ''
    and @endDate = ''
    group by c.fname 
    order by 换货金额 desc select * from #temp drop table #temp GO 
    这样不就行了吗!!!
      

  8.   

    ALTER  proc proc_bosChangeItem( 
    @beginDate datetime=null, 
    @endDate datetime=null ) 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between isnull(@beginDate,'1753-1-1') and isnull(@endDate,'9999-12-31 23:59:59') 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 
      

  9.   

    SELECT COUNT(*) from 表名 where 条件
      

  10.   

    直接判断传入参数是否为null,就得了。
    if(isnull(@beginDate,'') ='')select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp elseselect  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between isnull(@beginDate,'1753-1-1') and isnull(@endDate,'9999-12-31 23:59:59') 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
      

  11.   

    楼主,注意参数设置默认值,有了默认值就可以不用必须输入参数了
    还有就是codeb.fdate between isnull(@beginDate,b.fdate) and isnull(@endDate,b.fdate) 这种很经典的方法可以使用一下
      

  12.   

    ---在master数据库上直接执行即可
    create  proc proc_bosChangeItem

    @beginDate datetime = null, 
    @endDate datetime =null

    as 
    set nocount on 
    declare @MinDate as datetime
    declare @MaxDate as datetime
    select @MinDate=min(crdate) from sysobjects---表中出现的最小日期
    select @MaxDate=max(crdate) from sysobjects---表中出现的最大日期
    select * 
    from sysobjects 
    where 
    crdate between isnull(@beginDate,@MinDate) and isnull(@endDate,@MaxDate)
    GO exec proc_bosChangeItem ----不限定日期,查询表中所有的数据
    exec proc_bosChangeItem default,'2008-08-08'---限定截至日期
    exec proc_bosChangeItem '2008-08-08',default----限定开始日期
    ----------
    楼主的可以根据自己的要求,来参考下。
      

  13.   

    给个例子吧 SQL2005的 
    你看明白了就知道怎么做了
    没细看你的东西,所以没有直接写你要的答案ALTER PROC [dbo].[WMStkinMtManage]
    @WMStkinNo VARCHAR(20),
    @WMStkinType varchar(10),
    @WMStkinStatus VARCHAR(10),
    @BeginDate VARCHAR(20),
    @EndDate VARCHAR(20)
    AS
    BEGIN
      SELECT a.WMStkinNo, b.SectionValue AS WMStkinTypeText ,c.SectionValue AS WMStkinStatusText,
             a.RelatNo, a.SupplierNo,a.Fee, a.Memo,
             a.CreateTime, a.CreatePerNo, a.CreatePerName, 
             a.UpdateTime, a.UpdatePerNo, a.UpdatePerName, 
             a.AuditTime, a.AuditPerNo, a.AuditPerName, 
             a.CancelTime, a.CancelPerNo, a.CancelPerName
      FROM  WMStkinMt a JOIN dbo.MDRefDataDt b ON a.WMStkinType = b.SectionNo 
        AND b.ObjectNo = 'WMStkinType' 
        JOIN MDRefDataDt c on a.WMStkinStatus = c.SectionNo
        AND c.ObjectNo = 'WMStkinStatus' 
      WHERE CreateTime BETWEEN @BeginDate AND @EndDate
        AND (CASE WHEN ISNULL(@WMStkinNo, '') = '' THEN 1
                  WHEN a.WMStkinNo LIKE '%'+ @WMStkinNo +'%' THEN 1 
             END ) = 1
        AND (CASE WHEN ISNULL(@WMStkinType, '') = '' THEN 1
                  WHEN a.WMStkinType = @WMStkinType THEN 1 
             END ) = 1
        AND (CASE WHEN ISNULL(@WMStkinStatus, '') = '' THEN 1
                  WHEN a.WMStkinStatus = @WMStkinStatus THEN 1 
             END ) = 1
    END
      

  14.   

    ALTER  proc proc_bosChangeItem( 
    @beginDate datetime, 
    @endDate datetime ) 
    as 
    set nocount on 
    if @beginDate is not null and @endDate is not null 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    else if @beginDate is not null 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate>=@beginDate
    group by c.fname 
    order by 换货金额 desc 
    else if @endDate is not null 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate<=@endDate
    group by c.fname 
    order by 换货金额 desc 
    else
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO
      

  15.   

    不会吧 不是给了一个例子了吗?
    还得给写一个???例如:我写了一个存储过程有两参数如何不输入开始日期和结束日期就就查出所有记录 
    SET QUOTED_IDENTIFIER ON 
    GO 
    SET ANSI_NULLS ON 
    GO ALTER  proc proc_bosChangeItem( 
    @beginDate datetime, 
    @endDate datetime ) 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 AND (CASE WHEN @beginDate ISNULL THEN 1
             WHEN b.fdate >= @beginDate THEN 1 END)  = 1
    AND (CASE WHEN @beginDate ISNULL THEN 1
             WHEN b.fdate >= @ endDate  THEN 1 END)  = 1group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 这样能明白吗?
      

  16.   

    (b.fdate between @beginDate and @endDate  or (@begindate is null  and @endDate is null))
      

  17.   

    判断一下日期是不是为NULL,如果是,那么就把日期的谓词拿掉就行了,用得着这么麻烦吗,拿掉之后的效率还更高一些
    随便说一下,这个有必要用到临时表吗,有点画蛇添足
      

  18.   


    ALTER  proc proc_bosChangeItem( 
    @beginDate datetime='1900-01-01', 
    @endDate datetime =getdate()) 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 
    设置默认值
      

  19.   

    ALTER  proc proc_bosChangeItem( 
    @beginDate datetime = null, 
    @endDate datetime = null

    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and (b.fdate between @beginDate and @endDate OR 0 = isnull(@beginDate,0) OR 0 = isnull(@endDate,0))
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO
    PS:(b.fdate between @beginDate and @endDate OR 0 = isnull(@beginDate,0) OR 0 = isnull(@endDate,0))
    这个只要@beginDate和@endDate任意一个为空或全为空,都会返回所有记录,而且没有具体年份的限制。
      

  20.   

    或者使用动态SQL,如果传的参数为null(看你自己设定的不传参数是怎么定义的),写动态SQL排除该条件就可以了
      

  21.   


    --设置默认值ALTER  proc proc_bosChangeItem( 
    @beginDate datetime=0, 
    @endDate datetime='9999-12-31') 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
      

  22.   

    SET QUOTED_IDENTIFIER ON 
    GO 
    SET ANSI_NULLS ON 
    GO ALTER  proc proc_bosChangeItem( 
    @beginDate datetime='1900-01-01', 
    @endDate datetime='2999-01-01' ) 
    as 
    set nocount on 
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 
      

  23.   

    设置默认值了,开始日期为'1900-01-01',结束日期为'3000-01-01'@beginDate datetime='1900-01-01', 
    @endDate datetime='3000-01-01'
      

  24.   

    SET QUOTED_IDENTIFIER ON 
    GO 
    SET ANSI_NULLS ON 
    GO ALTER  proc proc_bosChangeItem( 
    @beginDate datetime, 
    @endDate datetime ) 
    as 
    set nocount on 
    set @beginDate = isnull(@beginDate, CONVERT(DATETIME, '1753-01-01', 121))
    set @endDate = isnull(@endDate, CONVERT(DATETIME, '9999-12-31 23:59:59.998', 121))
    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and b.fdate between @beginDate and @endDate 
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO 
      

  25.   

    你的问题我也遇到过。楼上有人说过取默认值,我也采用了这种方法,
    between (isnull(@beginDate,'1900-01-01')='1900-01-01' or a.beginDate=@beginDate) and (isnull(@endDate,getdate())=getdate() or a.endDate=@endDate)
      

  26.   

    select  IDENTITY(int,1,1) AS 名次, c.fname as 部门,sum(a.famount) as 换货金额 ,sum(a.fqty) 换货数量 into #temp from t_boschangeitementry a 
    left join t_boschangeitem b on a.fid=b.fid 
    left join t_department c on b.fdeptid=c.fitemid 
    where b.fchecker>0 and 
    (@beginDate is null or b.fdate >=@beginDate )
    and ( @endDate is null or b.fdate <= @endDate )
    group by c.fname 
    order by 换货金额 desc 
    select * from #temp 
    drop table #temp 
    GO