日期:2014-05-16  浏览次数:20989 次

Access中能自定义函数吗?如何将SQLServer中用户定义函数转到Access中?
Access中能自定义函数吗?
下面的SQLServer中用户定义函数,如何转到Access中?

CREATE   function   f_cid_Cata(@id   int
)returns   @re   table([CataNo]   int,[CataName]   varchar(50),[ParentNo]   int,[level]   int)
as
begin
declare   @l   int
set   @l=0
insert   @re   select   CataNo,CataName,ParentNo,Null   from   PicCata   where   CataNo=@ID
while   @@rowcount> 0
begin
set   @l=@l+1
insert   @re   select   a.CataNo,a.CataName,a.ParentNo,@l
from   [PicCata]   a,@re   b
where   a.[ParentNo]=b.[CataNo]   and   a.CataNo   not   in   (select   CataNo   from   @re)
end
return
end



------解决方案--------------------
首先明确:

Access使用的是Jet-SQL,SQL Server使用的是T-SQL,两者用法上相差很大。


JET SQL 帮助(jet4 access2000)下载地址

http://www.access911.net/index.asp?board=8&recordid=75FAB71E&tt=


------解决方案--------------------
SQL Server的后台函数直接返回一个记录集,而Access需要在“模块”中新建一个函数,用VBA代码处理。

两者使用有着非常大的区别。

Access的函数只能返回数值,而不是集合,但可以在函数中将需要的数据保存到表中。
------解决方案--------------------
有UDF,但你的函数不能直接转换,T-SQL与JET SQL是不同的,你返回的是一
TABLE数据类型变量,在ACCESS中是没有的,只有通过VBA来实现。
------解决方案--------------------
实际上SQL SERVER的存储过程与VBA编程相差不大,只不过具体的语言形式不同而已,
思路基本一致。
------解决方案--------------------
没有其他更好的办法
------解决方案--------------------
在 ACCESS 中在函数中直接建立个表不就得了