2012-09-21 81 views
0

可能重複一個用戶定義的標量函數:
Cast integer and concatenate to varchar in TSQL呼叫內的執行語句

我已經創建了兩個標量函數,每個返回uniqueidentifer類型。我需要在執行語句中調用這些過程,但我似乎有語法錯誤。

Exec spModifyProductPropertyValue 
    @PAVID, 
    fnGetPropertyIDbyLabel(@Label7, @USPID, 0, 1), 
    @ProductID, 
    @Value7, 
    fnGetPINID(@7PIN), 
    0, 
    @counter out 

我注意到那是什麼,當我單獨調用的函數,然後通過使用來自theprevious調用的輸出上面的號召,像這樣

Declare 
     @PropertyID as uniqueidentifier = null 

Select @PropertyID = fnGetPropertyIDbyLabel(@Label7, @USPID, 0, 1) 

Exec spModifyProductPropertyValue 
     @PAVID, 
     @PropertyID, 
     @ProductID, 
     @Value7, 
     fnGetPINID(@7PIN), 
     0, 
     @counter out 

編譯器不會抱怨太多。這種方法的問題是我最終創建了很多這些臨時變量(差不多50),這是我想盡量避免的。我希望得到這個正確的任何幫助,請。

+0

閱讀此:http://stackoverflow.com/questions/5967035/using-function-as-a-parameter-when-executing-a-stored-procedure。這是不可能的。 – prashanth

回答

-1

你需要有主人的前綴:

Exec spModifyProductPropertyValue 
    @PAVID, 
    dbo.fnGetPropertyIDbyLabel(@Label7, @USPID, 0, 1) as GetPropertyID, 
    @ProductID, 
    @Value7, 
    dbo.fnGetPINID(@7PIN) as GetPINID, 
    0, 
    @counter out 

Awnser在這裏:SQL User Defined Function Within Select

+0

這樣會導致不正確的語法錯誤。 – Kobojunkie

0

的語法UDF

expression... function_name(value_1, value_2, ... value_n)... 

所以,你只能傳遞值,而不是表達式。

由於使用函數是一個表達式, 另一功能

 Exec spModifyProductPropertyValue 
    @PAVID, 
    dbo.fnGetPropertyIDbyLabel(@Label7, @USPID, 0, 1) as GetPropertyID, --Expression 
    (..) 

的呼叫內是不可能的。

所以,是的,你將不得不創建變量。但如果所有的都是int,也許你可以創建一個並在每次使用函數時使用它。 或者修改你的函數只做兩件功能所做的事情。