2016-04-12 10 views
0

Ich versuche, eine Funktion wie die unten zu erstellen, im Wesentlichen muss ich für zwei Geld Werte abfragen und fügen Sie sie dann zusammen und das Ergebnis zurück. Ist das möglich?Hinzufügen von zwei Abfragen in SQL-Funktion

Dank

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

Wäre es nicht genug sein, Klammern um die Selects selbst (SELECT ..) + (SELECT ...) hinzufügen? –

+0

Danke Allan, das war's! Dummer Fehler. – Ben

Antwort

0

wie die Sie interessieren ..

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

Dieses mit weniger Code getan werden kann, aber das gibt Ihnen die allgemeine Idee:

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()