在自定义函数中声明多个变量

时间:2017-07-17 07:14:19

标签: sql sql-server

我正在尝试创建一个函数。该功能将接收两个参数&我希望它能返回一个字符串。

例如

    Paramter 1          Parameter 2
    @CurrName           @Currency

    SEP17 3600 CALL     GBP
    AUG17 4000 CALL     EUR

所以我的函数将采用两个参数,请注意参数1 @CurrName将始终为3个字母(月份)和2个数字,我们不关心。

所以我想如果@FX是GBP将字符串的开头返回为“UKX”,如果是EUR,那么“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 个答案:

答案 0 :(得分:1)

使用此:

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