ALTER FUNCTION testGetSubNodes
(
@nodeId int
)
RETURNS @t TABLE
(
id bigint identity(1,1) not null,
nodeIds int,
nodeName varchar(500)
)
AS
BEGIN
insert into @t values(@nodeId,'header');
while exists(select nodeid from dbo.Tree where parentid in (select nodeIds from @t) and nodeid not in(select nodeIds from @t))
begin
insert into @t select nodeid, nodename from dbo.Tree where parentid in (select nodeIds from @t)
end
RETURN
END
这个函数的主要功能就是返回当前节点下的所有子节点,在存储过程中写select * from testGetSubNodes(nodeId)就可以返回表中的数据了。再写一个标量值函数
ALTER FUNCTION [dbo].[testGetSubNodes_]
(
@nodeId int
)
RETURNS int
AS
BEGIN
declare @nodeCount int
select @nodeCount=5 from MenuTree
return @nodeCount
END
这个函数很简单返回一个整型值,然后就可以在存储过程中调用了,不过调用的方式有所不同,象上面的表值函数调用是不需要所有者的,只要写函数名称就可以,对于标量值函数来说,是需要加上所有者的,比如所有者是dboselect dbo.testGetSubNodes_,这样就可以返回5,如果不加dbo,那sql会不认识这个函数。