函数TSQL中的函数 [英] Function within a Function TSQL
问题描述
我可以在表值函数中调用标量函数吗?
谢谢 是的,只要table-
用户定义的函数可以嵌套;
即一个用户定义的函数可以
调用另一个函数。当被调用的函数
开始执行时,嵌套级别为
,当
被调用的函数完成
执行时,该级别递减。用户定义的函数可以
嵌套到32个级别。超过
嵌套的最大级别导致
整个调用函数链到
失败。从Transact-SQL用户定义的
函数对托管代码
的任何引用都将作为一个级别对
的32级嵌套限制进行计数。方法在托管代码中调用
不计入此限制。
http://msdn.microsoft.com/en-us/library/ms186755.aspx
这非常简单,但它确实有效:
- DROP FUNCTION RETURN_INT
--GO
CREATE FUNCTION RETURN_INT()
RETURNS INT
WITH EXECUTE AS CALLER
AS
BEGIN
RETURN 1
END
GO
--DROP FUNCTION RETURN_TABLE
--GO
CREATE FUNCTION RETURN_TABLE()
RETURNS @Test TABLE (
ID INT
)
执行呼叫者
AS
BEGIN
INSERT INTO @Test
SELECT DBO.RETURN_INT ()
RETURN
END
Can I call a scalar function within a table-valued function?
Thanks
Yes, just as long as the table-valued function returns a table when it's done.
User-defined functions can be nested; that is, one user-defined function can call another. The nesting level is incremented when the called function starts execution, and decremented when the called function finishes execution. User-defined functions can be nested up to 32 levels. Exceeding the maximum levels of nesting causes the whole calling function chain to fail. Any reference to managed code from a Transact-SQL user-defined function counts as one level against the 32-level nesting limit. Methods invoked from within managed code do not count against this limit.
http://msdn.microsoft.com/en-us/library/ms186755.aspx
This is very simplistic, but it does work:
--DROP FUNCTION RETURN_INT
--GO
CREATE FUNCTION RETURN_INT ()
RETURNS INT
WITH EXECUTE AS CALLER
AS
BEGIN
RETURN 1
END
GO
--DROP FUNCTION RETURN_TABLE
--GO
CREATE FUNCTION RETURN_TABLE ()
RETURNS @Test TABLE (
ID INT
)
WITH EXECUTE AS CALLER
AS
BEGIN
INSERT INTO @Test
SELECT DBO.RETURN_INT()
RETURN
END
这篇关于函数TSQL中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!