2016-04-12 11 views
0

私は以下のような関数を作成しようとしていますが、基本的に2つの金額を照会し、それらを合計して結果を返す必要があります。それは可能ですか?SQL関数で2つのクエリを追加する

おかげ

CREATE function [dbo].[fn_manualpricing2-NEW](@type varchar(50),@capid int,@milespa int, @maintained bit, @term int, @product varchar(50), @funder varchar(50)) 
returns money 
as 
BEGIN 

RETURN (
SELECT TOP 1 price FROM tblPricing WHERE [type][email protected] AND [email protected] AND [email protected] AND [email protected] AND [email protected] AND [email protected] and [email protected] ORDER BY price 
+ 
SELECT TOP 1 addonpricevalue FROM [dbWebsiteLO3-PRICING].[dbo].[AddonPrices] WHERE VehicleType=1 AND [email protected] AND [email protected] AND [email protected] 
) 
END 

GO 
+0

それはありません選択自体の周りに括弧を追加するのに十分である(SELECT ..)+(SELECT ...)? –

+0

アラン、ありがとう!愚かな間違い。 – Ben

答えて

0

は次のように試してみてください。..

CREATE function [dbo].[fn_manualpricing2-NEW](@type varchar(50),@capid int,@milespa int, @maintained bit, @term int, @product varchar(50), @funder varchar(50)) 
returns money 
as 
BEGIN 

DECLARE @variable money 
SET @variable = (SELECT TOP 1 price FROM tblPricing WHERE [type][email protected] AND [email protected] AND [email protected] AND [email protected] AND [email protected] AND [email protected] and [email protected] ORDER BY price) 
+ 
(SELECT TOP 1 addonpricevalue FROM [dbWebsiteLO3-PRICING].[dbo].[AddonPrices] WHERE VehicleType=1 AND [email protected] AND [email protected] AND [email protected]) 

return @variable 

END 

GO 
0

これは、少ないコードで行うことができますが、これはあなたの一般的なアイデアを与える:

CREATE FUNCTION dbo.ufn_AddTwoValues() 
RETURNS Money 
WITH EXECUTE AS CALLER 
AS 
-- place the body of the function here 
BEGIN 
    DECLARE @ValOne money; 
    DECLARE @ValTwo money; 
    DECLARE @Result money; 

    SET @ValOne = (SELECT 1); 
    SET @ValTwo = (SELECT 2); 

    SET @Result = @ValOne + @ValTwo; 

    RETURN @Result; 
END 
GO 

select dbo.ufn_AddTwoValues()