+ -
当前位置:首页 → 问答吧 → 一个简单的问题

一个简单的问题

时间:2011-12-14

来源:互联网

--自定义一个函数,要求函数计算给定出生日期的年龄:
create function fun_age(@birth as datetime)
returns int
as
begin
declare @age int
set @age=datediff(yy,@birth,getdate())
--set @age=year(getdate())-year(@birth)
return @age
--return (year(getdate())-year(@birth))
end

drop function fun_ageselect dbo.fun_age('1990-04-23') as 年龄
为什么在2000版本上报错(2005以上都可以),提示getdate()函数
错误。请问有没有方法能在2000版本上正确使用这个函数

作者: TravyLee   发布时间: 2011-12-14

因为2000里的自定义函数内不能使用GETDATE(),而2005可以.

作者: liangCK   发布时间: 2011-12-14

是不可以的

需要用一個視圖

SQL code
Create view Dt
as
select dt=getdate()


函數調用視圖實現

作者: roy_88   发布时间: 2011-12-14

SQL code
CREATE VIEW v_GETDATE
AS
    SELECT GETDATE() AS curDate;
GO


create function fun_age(@birth as datetime)
returns int
as 
begin
declare @age int

SELECT @age=datediff(yy,@birth,curDate)
FROM v_GETDATE;

return @age
end

作者: liangCK   发布时间: 2011-12-14