日期:2014-05-17  浏览次数:20496 次

怎么指定动态的GETDATE
select CIFNm,ContractNm,LoanBlankDate,PeriodNm,'PeriodMangeCharge' as RepayType,PeriodMangeCharge as RepayAmount,
LoanBalance,TotalInterest,RemainInterest,RemainPeriod,IsAdvanceBlack,'0' as bk from InterestList
) AS a 
where convert(varchar(10),LoanBlankDate,120) =convert(varchar(10),GETDATE(),120) and IsAdvanceBlack='False'

我的存储过程中有这么一句话。


convert(varchar(10),GETDATE(),120)

现在,我想在程序里调用存储过程,
但是GETDATE需要时可能是一个变量,比如:2012-8-27-2012-9-12
请问怎么写。

------解决方案--------------------
你是要把getdate替换成某一个日期还是一个日期区间呢?
如果是某一个日期,那么存储过程多接受个参数@Date
语句改成
SQL code

select CIFNm,ContractNm,LoanBlankDate,PeriodNm,'PeriodMangeCharge' as RepayType,PeriodMangeCharge as RepayAmount,
LoanBalance,TotalInterest,RemainInterest,RemainPeriod,IsAdvanceBlack,'0' as bk from InterestList
) AS a  
where convert(varchar(10),LoanBlankDate,120) =convert(varchar(10),@Date,120) and IsAdvanceBlack='False'

------解决方案--------------------
思维不要太跳跃

你想需要可变那就是参数,只是有时不传这个参数的时候用当前时间代替,那就是一个默认NULL的参数,在存储过程处理下参数,如果NULL用GETDATE()代替

SQL code
CREATE PROC PR_TEST (
@DT DATETIME = NULL
)
AS

IF @DT IS NULL
     SET @DT = GETDATE()

...

GO