我们可以从函数调用存储过程吗?

时间:2022-08-01 19:41:28

Can we call a stored procedure from a function in SQL? What's the reason?

我们可以从SQL中的函数调用存储过程吗?什么原因?

4 个解决方案

#1


2  

Yes.

是。

You can do this with a bit of a hack involving openrowset but it's not recommended as it will open a new connection.

你可以通过涉及openrowset的一些黑客来做到这一点,但不建议这样做,因为它会打开一个新的连接。

CREATE FUNCTION dbo.test ()RETURNS  varchar(200)ASBEGIN    RETURN (Select top 1 [Name] from OPENROWSET('SQLNCLI','Server=.\SQL2008;Trusted_Connection=yes;','SET NOCOUNT ON;SET FMTONLY OFF;EXEC MASTER..SP_HELP') )ENDGOSELECT dbo.test()

#2


2  

Functions can only read data, they can't update or change anything. It follows that functions are not allowed to call stored procedures.

函数只能读取数据,不能更新或更改任何内容。因此,不允许函数调用存储过程。

#3


1  

Quick answer: No.

快速回答:没有。

Why: A stored procedure does not produce any output that can be re-used inside SQL.

原因:存储过程不会产生任何可以在SQL中重用的输出。

#4


0  

To be efficient a function should be deterministic, i.e. the output should only be depending on the input, so that the result can be cached.

为了有效,函数应该是确定性的,即输出应该仅取决于输入,以便可以缓存结果。

If you want to call a stored procedure from a function, yout have to specifically make the function non-deterministic.

如果要从函数调用存储过程,则必须专门使函数不确定。

#1


2  

Yes.

是。

You can do this with a bit of a hack involving openrowset but it's not recommended as it will open a new connection.

你可以通过涉及openrowset的一些黑客来做到这一点,但不建议这样做,因为它会打开一个新的连接。

CREATE FUNCTION dbo.test ()RETURNS  varchar(200)ASBEGIN    RETURN (Select top 1 [Name] from OPENROWSET('SQLNCLI','Server=.\SQL2008;Trusted_Connection=yes;','SET NOCOUNT ON;SET FMTONLY OFF;EXEC MASTER..SP_HELP') )ENDGOSELECT dbo.test()

#2


2  

Functions can only read data, they can't update or change anything. It follows that functions are not allowed to call stored procedures.

函数只能读取数据,不能更新或更改任何内容。因此,不允许函数调用存储过程。

#3


1  

Quick answer: No.

快速回答:没有。

Why: A stored procedure does not produce any output that can be re-used inside SQL.

原因:存储过程不会产生任何可以在SQL中重用的输出。

#4


0  

To be efficient a function should be deterministic, i.e. the output should only be depending on the input, so that the result can be cached.

为了有效,函数应该是确定性的,即输出应该仅取决于输入,以便可以缓存结果。

If you want to call a stored procedure from a function, yout have to specifically make the function non-deterministic.

如果要从函数调用存储过程,则必须专门使函数不确定。