create FUNCTION [dbo].[setSP] ()
RETURNS datetime AS
begin
Declare @date datetime
.....
set @date= getdate()
.....
return @date
end
出现如下错误
在函数内不正确地使用了 'getdate'。
怎么解决?
...全文
2453打赏收藏
在sql的自定义函数中,怎样使用getdate()?
create FUNCTION [dbo].[setSP] () RETURNS datetime AS begin Declare @date datetime ..... set @date= getdate() ..... return @date end 出现如下错误 在函数内不正确地使用了 'getdate'。 怎么解决?
I did briefly discuss of using non-determinitic functions in UDF is not possible. But there have been tons of questions in newsgroups asking why they cannot use getdate() in their UDF. Understand that getdate() is a built-in UDF which is non-determinitic in nature. But there are workarounds for the same. See the example below :
CREATE VIEW CurrentDate AS SELECT Date = GETDATE()
GO
CREATE FUNCTION myFunction () RETURNS DATETIME AS
BEGIN
DECLARE @myDateTime DATETIME
SELECT @myDateTime = Date FROM CurrentDate
RETURN (@myDateTime)
END
GO
SELECT dbo.myFunction()
GO