是否可以在存储过程参数中设置当前日期?

时间:2013-08-13 07:44:06

标签: sql-server-2008 tsql datetime

我想将GetDate()设置为默认参数值。但它显示错误。下面是我的代码块:

Create Proc XYS
(
   @myDate datetime = GetDate()  //error

)
AS
SET @myDate = GetDate() // i can do this but neither i want this nor I want to 
                           pass current date from front end or upper layer


....
....

据我所知,不支持函数/动态值作为此级别,而是允许硬编码值。任何解决方法?

2 个答案:

答案 0 :(得分:1)

建议使用一个值(例如0 =最小日期时间),然后在存储过程中检查它:

Create Proc XYS
(
   @myDate datetime = 0)
AS
BEGIN
    IF @myDate = 0
        SET @myDate = GetDate()

    // ...
END

答案 1 :(得分:0)

我不是说这是世界上最好的主意,但它对我有用:

CREATE VIEW dbo.V_GetDate
AS
SELECT     GETDATE() AS TheDate

然后

Create Proc XYS
AS
declare @myDate datetime
Select @myDate = TheDate from dbo.V_GetDate