2012-11-28 31 views
0

存儲我創建了這樣一個簡單的存儲過程的過程參數錯誤

Alter Proc dbo.s5 
(
    @id int = 14, 
    @Salary int output 
) 
As 
Begin 
    --Declare @Salary int 
    set @Salary = (select Salary 
       from dbo.EmpInf 
       where EmpId = @id) 
    print @Salary 
    --Return @Salary 
End 

在此存儲過程的執行,我得到這個錯誤:

Msg 201, Level 16, State 4, Procedure s5, Line 0
Procedure or function 's5' expects parameter '@Salary', which was not supplied.

我在做什麼錯?

回答

1

您需要將@Salary變量作爲輸入變量提供給存儲過程,即使它被標記爲輸出參數。

exec dbo.s5 @id, @salary 

如可以在以下link閱讀:

Input values can also be specified for OUTPUT parameters when the stored procedure is executed. This allows the stored procedure to receive a value from the calling program, change it or perform operations with it, then return the new value to the calling program. ... This is often referred to as "pass-by-reference capability.

+2

利用輸出參數返回數據: http://msdn.microsoft.com/en-us/library/ms187004(v= sql.105).aspx – cbillowes

+0

Ma'am我通過提供Exec dbo.s5 14,5來執行sp。現在它正確執行。但是如果我們標記爲Output,那麼給Output變量提供一個值的意義何在。 –

+2

如果您在執行存儲過程之前聲明@Salary變量並在執行之後將其打印出來,則變量將包含存儲過程中指定的OUTPUT值。然後,您可以在您的服務器端代碼或您打算使用它的任何地方使用此變量的值。注意:我已經更新了我的答案,以包含來自上面鏈接的引用文本解釋它。 – cbillowes