调用多标量表值函数中的存储过程? [英] Call a stored procedure in multi scalar table-valued function?

查看:568
本文介绍了调用多标量表值函数中的存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像多标量表值函数那样调用存储过程?

注意:这个查询返回错误:


在函数中无效使用了副作用运算符'INSERT EXEC'。


T-SQL代码:

  CREATE FUNCTION fnCrSum 

@ay nvarchar(100),@yil nvarchar(4),@tip nvarchar(25)

RETURNS
@tablo TABLE

id int,
ref_num int,
last_name nvarchar(200),
grup nvarchar(200),
analist nvarchar(200),
durum nvarchar(10),
start_dt int,
end_dt int,
持续时间int,
类型nvarchar(10),
bildirim nvarchar(200)

AS
BEGIN
DECLARE @exec NVARCHAR(250)
SET @exec ='exec dbo.spCrSum'''+ @ay +''','''+ @yil +''','''+ @tip +''''
I NSERT INTO @tablo
exec(@exec)

RETURN
END
GO


解决方案

函数不能有副作用,即更改数据。

即使你的存储过程没有改变任何东西,SQL编译器也不能假设,所以它不允许它。



为什么你需要将它包装在一个函数中?


Is there any way to call a stored procedure in multi scalar table-valued function like that?

Note: this query returns error:

Invalid use of a side-effecting operator 'INSERT EXEC' within a function.

T-SQL code:

CREATE FUNCTION fnCrSum
(
@ay nvarchar(100), @yil nvarchar(4), @tip nvarchar(25)
)
RETURNS 
@tablo TABLE 
(
    id int,
    ref_num int,
    last_name nvarchar(200),
    grup nvarchar(200),
    analist nvarchar(200),
    durum nvarchar(10),
    start_dt int , 
    end_dt int,
    duration int,
    type nvarchar(10),
    bildirim nvarchar(200)
)
AS
BEGIN
    DECLARE @exec NVARCHAR(250)
    SET @exec = 'exec dbo.spCrSum ''' + @ay + ''', ''' + @yil + ''', ''' +  @tip + ''''
    INSERT INTO @tablo
    exec (@exec)

    RETURN 
END
GO

解决方案

Functions can't have side effects i.e. change data.

Even if your stored procedure doesn't change anything, the SQL compiler can't assume that, So it just doesn't allow it.

Why do you need to wrap this in a function?

这篇关于调用多标量表值函数中的存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆