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

请教时间条件的判断该怎么写呢

2012-10-16 
请问时间条件的判断该如何写呢?我写了一个存储过程,现在查询条件里有2个时间条件,如果2个时间都为空,就查

请问时间条件的判断该如何写呢?
我写了一个存储过程,现在查询条件里有2个时间条件,
如果2个时间都为空,就查询出所有记录;
如果起始时间为空,就查询出到截止时间的记录;
如果截止时间为空,就查询从起始时间开始的记录。

已经写了一部分,但是不知道怎么写了,请大家指点指点,谢谢!

SQL code
--创建存储过程Alter PROC ICPM_Expeceted(    @FromTime smalldatetime,    @ToTime smalldatetime,    @userId int ='',    @LesseeTypeId int ='',    @SortField Nvarchar(200),    @SortBy Nvarchar(20))AS--如果存在临时表就删除临时表IF EXISTS(SELECT [Name] FROM tempdb.dbo.sysobjects WHERE id = OBJECT_ID('tempdb..#tblExpecetdScoreTemp'))    DROP TABLE #tblExpecetdScoreTemp    SELECT    RowId = IDENTITY(int,1,1),    T.CustomerId,    U.UserName,        C.CustomerName,    T.IntentScore,    T.ContractScore,    T.RentScore,    T.StoreTypeScore,    T.LesseeTypeScore,    T.TotalScoreINTO #tblExpecetdScoreTempFROM(SELECT UserId, CustomerId,IntentScore,ContractScore,RentScore,StoreTypeScore,LesseeTypeScore,TotalScore FROM tblExpecetdScoreWHERE(    (@userId <> '' and userId = @userId)    OR    (@userId = ''))AND((@LesseeTypeId <> '' and LesseeType = @LesseeTypeId)    OR    (@LesseeTypeId = ''))AND(     (@FromTime = '' and @ToTime = '')         OR         (          contractDate between @FromTime and @ToTime          )  --是在这里写的吧,请补充一下    ))T join tblUser as U  on T.UserId = U.UserIdjoin tblCustomer as C on T.CustomerId = C.CustomerIdGroup by     U.UserName,    T.UserId,    T.CustomerId,    C.CustomerName,    T.IntentScore,    T.ContractScore,    T.RentScore,    T.StoreTypeScore,    T.LesseeTypeScore,    T.TotalScoreOrder by T.TotalScore ASC


[解决办法]
写成动态SQL,时间条件子句根据两个时间变量判断后再拼接:
SQL code
Alter PROC ICPM_Expeceted(    @FromTime smalldatetime,    @ToTime smalldatetime,    @userId int ='',    @LesseeTypeId int ='',    @SortField Nvarchar(200),    @SortBy Nvarchar(20))AS--如果存在临时表就删除临时表IF EXISTS(SELECT [Name] FROM tempdb.dbo.sysobjects WHERE id = OBJECT_ID('tempdb..#tblExpecetdScoreTemp'))    DROP TABLE #tblExpecetdScoreTempDECLARE @sql VARCHAR(8000),        @where VARCHAR(2000)SET @sql='SELECT                    RowId = IDENTITY(int,1,1),                    T.CustomerId,                    U.UserName,                        C.CustomerName,                    T.IntentScore,                    T.ContractScore,                    T.RentScore,                    T.StoreTypeScore,                    T.LesseeTypeScore,                    T.TotalScore                INTO #tblExpecetdScoreTemp                FROM                (                SELECT UserId, CustomerId,IntentScore,ContractScore,RentScore,StoreTypeScore,LesseeTypeScore,TotalScore FROM tblExpecetdScore '                IF (@FromTime='' AND @ToTime<>'')BEGIN    SET @where=' WHERE 日期字段<='+CONVERT(VARCHAR(10),@FromTime,120)ENDIF (@ToTime='' AND @FromTime<>'')BEGIN    SET @where=' WHERE 日期字段>='+CONVERT(VARCHAR(10),@ToTime,120)ENDIF (@FromTime<>'' AND @ToTime<>'')BEGIN    SET @where=' WHERE 日期字段 between '+CONVERT(VARCHAR(10),@FromTime,120)+' and '+CONVERT(VARCHAR(10),@ToTime,120)ENDSET @sql=@sql+@where    SET @sql+')                )T                 join tblUser as U  on T.UserId = U.UserId                join tblCustomer as C on T.CustomerId = C.CustomerId                Group by                     U.UserName,                    T.UserId,                    T.CustomerId,                    C.CustomerName,                    T.IntentScore,                    T.ContractScore,                    T.RentScore,                    T.StoreTypeScore,                    T.LesseeTypeScore,                    T.TotalScore                Order by T.TotalScore ASC ' PRINT @sqlEXEC (@sql)
[解决办法]
SQL code

where contractDate between (case when isnul(@FromTime,'')='' then '1900-1-1' else @FromTime end) and (case when isnull(@ToTime,'')='' then '2100-1-1' else @ToTime end)
[解决办法]
不好意思忘了个“and”
SQL code
DECLARE @sql VARCHAR(8000)       SET @sql='SELECT                    RowId = IDENTITY(int,1,1),                    T.CustomerId,                    U.UserName,                        C.CustomerName,                    T.IntentScore,                    T.ContractScore,                    T.RentScore,                    T.StoreTypeScore,                    T.LesseeTypeScore,                    T.TotalScore                INTO #tblExpecetdScoreTemp                FROM                (                SELECT UserId, CustomerId,IntentScore,ContractScore,RentScore,StoreTypeScore,LesseeTypeScore,TotalScore FROM tblExpecetdScore                  where 1=1 '                IF (@FromTime<>'' )BEGIN    SET @sql=@sql+' and 日期字段>='+CONVERT(VARCHAR(10),@FromTime,120)ENDIF (@ToTime<>'' )BEGIN    SET @sql=@sql+' and 日期字段<='+CONVERT(VARCHAR(10),@ToTime,120)END 

热点排行
Bad Request.