急!急!急!关于用户自定义函数的问题???

King6860 2004-07-31 09:45:51
为什么在用户定义的函数内,GETDATE()函数就不能用了???

create function fnKeHuXiaoShouJingYing
(@timeStyle varchar(1),@dealNo varchar(20),@sortNo varchar(20)=null,@xsth int=0)
returns money
begin

declare @rtValue money

if @sortNo is null
begin
if @timeStyle='d'
begin
select @rtValue=isNull(sum(dsMoney),0) from dealerSale
where datediff(dd,getDate(),selldate)=0
and sellFlag=@xsth and dealNo =@dealNo
end
return @rtValue
end

错误信息:
服务器: 消息 443,级别 16,状态 1,过程 fnKeHuXiaoShouJingYing,行 27
在函数内不正确地使用了 'getdate'。
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
King6860 2004-07-31
  • 打赏
  • 举报
回复
那如果创建视图的话,函数中的参数怎么传递???
tx1icenhe 2004-07-31
  • 打赏
  • 举报
回复
自定义函数不能使用getdate等不确定函数
所以用一个视图vi_getdate来躲过这个规定

tx1icenhe 2004-07-31
  • 打赏
  • 举报
回复
create view vi_getdate
as
select getdate() as now
go

create function fnKeHuXiaoShouJingYing
(@timeStyle varchar(1),@dealNo varchar(20),@sortNo varchar(20)=null,@xsth int=0)
returns money
begin

declare @rtValue money

if @sortNo is null
begin
if @timeStyle='d'
begin
select @rtValue=isNull(sum(dsMoney),0) from dealerSale
where datediff(dd,(select now from vi_getdate),selldate)=0
and sellFlag=@xsth and dealNo =@dealNo
end
end
return @rtValue
end
go

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧