我正在尝试将过程和函数结合起来以了解更多关于它们的信息。
我有一个名为
customer
的表,其中有几列(我将使用的列是 sal
)。
我创建了一个函数来检查谁的工资低于 25000。如果上述情况呈现行,那么我从我的函数中调用该过程。
该过程更新
sal
(sal = sal + 1000
) 并返回行计数。
create procedure Taxrefund2(@taxr int) as
begin
update customer
set Balance=Balance + @taxr
return @@rowcount
end
create function taxfunc()
as
begin
declare @salary table(sal decimal(10,2))
set @salary = (select sal from customer)
declare @x int=0
if @salary < 25000
exec @x = taxrefund2(1000)
return @x
print 'the no of customers who got tax redeemption is :' +cast(@x as varchar(10))
当我编译我的函数时,我收到错误:
消息 156,第 15 级,状态 1,程序taxfunc,第 2 行
关键字“as”附近的语法不正确。消息 102,第 15 级,状态 1,程序taxfunc,第 8 行
“1000”附近的语法不正确。消息 178,第 15 级,状态 1,程序taxfunc,第 9 行
在此上下文中不能使用带有返回值的 RETURN 语句。消息 102,第 15 级,状态 1,程序taxfunc,第 10 行
')' 附近的语法不正确。
有人可以解释一下我在代码或概念中做错了什么吗?
您不能直接从用户定义的函数调用存储过程。
可以通过 OPENROWSET 实现。请参阅以下链接了解更多详情。
https://www.sqlshack.com/execute-a-sql-server-procedure-inside-the-function/