2017-07-17 145 views
0

我正在嘗試創建一個函數。該函數將收到兩個參數&我希望它返回一個字符串。聲明自定義函數內的多個變量

例如

Paramter 1   Parameter 2 
    @CurrName   @Currency 

    SEP17 3600 CALL  GBP 
    AUG17 4000 CALL  EUR 

所以我的功能將需要兩個參數,請注意參數1 @CurrName永遠是3個字母(月)和2個,我們不關心數字。

所以我想,如果@FX是英鎊字符串的開頭返回到是「UKX」,如果是歐元則「SXE5」

我從這個想的輸出是這樣

1st one ->  UKX 9 3600 
2nd one -)  SXE5 8 4000 

但是我在第一位遇到問題,因爲它似乎不喜歡我聲明變量@tickStart的事實,「函數中包含的select語句無法將數據返回給客戶端」。

CREATE FUNCTION ufOptionName 
    (
    -- Add the parameters for the function here 
    @CurrName nvarchar(30), 
    @FX nvarchar(3) 
) 
    RETURNS nvarchar(30) 
    AS 
    BEGIN 
    -- Declare the return variable here 
DECLARE @newName nvarchar(30), 
@tickStart nvarchar(10) 

select case  
    when @FX = 'EUR' then set @tickStart = 'SX5E' 
    when @FX = 'GBP' then set @tickStart = 'UKX' 
    else set @tickStart = 'US' 
end 

-- Return the result of the function 
RETURN @newName 

END 
GO 

回答

1

使用此:

SET @tickStart = case  
    when @FX = 'EUR' then 'SX5E' 
    when @FX = 'GBP' then 'UKX' 
    else 'US' 
end