2013-11-20 151 views
1

我正在尝试结合程序和函数来了解更多关于它们的内容。调用函数内部函数

我有一个名为customer的表格,列数很少(我将使用的列为sal)。

我创建了一个函数来检查谁的工资都低于25000.如果上述情况呈现行,然后我从我的函数调用过程。

该过程更新salsal = sal + 1000)并返回rowcount。

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
不正确的语法。

Msg 102,Level 15,State 1,Procedure taxfunc,Line 8
'1000'附近的语法不正确。

消息178,级别15,状态1,过程taxfunc,第9行
带有返回值的RETURN语句不能在此上下文中使用。

Msg 102,Level 15,State 1,Procedure taxfunc,Line 10
')'附近的语法不正确。

有人能解释我在代码或概念中做了什么错误吗?

+1

不能调用SQL Server用户定义的函数内的存储过程。而且你不能在一个函数内改变数据库的状态,但是你在代码中试图做什么可以通过一个存储过程来实现:) –

+0

为什么你有SQL Server和MySql的标签? –

+0

[可以在用户定义的函数中调用存储过程吗?]可能的重复?(http://stackoverflow.com/questions/12843788/is-it-possible-to-call-a-stored-procedure-in - 一个用户定义的函数) –

回答

1

您不能直接从用户定义函数调用存储过程。

How to call a stored procedure from a user defined function In SQL Server 2000

+0

其实这不是完整的真实,有一个例外..看到我链接为一个副本的帖子.. –

+0

谁downvoted。我刚刚分享了这个问题的重复。 – goofyui

+0

不是downvoter,但为什么重复相同的答案?并且您还有一条评论指出您的答案存在问题..并且他没有使用SQL Server 2000 ... –