从存储过程中的存储过程获取值

时间:2016-08-25 08:48:56

标签: tsql stored-procedures sql-server-2014

我有一个存储过程,它返回一个XML文件。目前,一些计算是在XSL中完成的,但我想在数据库中使用另一个存储过程来完成这些计算。 (将计算结果添加到XML)

ALTER PROCEDURE [dbo].[app_Get_Phone_And_Tariffs] 
-- Add the parameters for the stored procedure here

@phone nvarchar(150)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here


SELECT

    PB.UID as '@phoneid',
    PB.Short_Title as '@title',
    PB.Description as '@desc',
    PB.Camera as '@camera',     
    PB.Storage as '@storage',
    PB.Screen_Size as '@screensize',
    PB.OS as '@os',
    PB.Processor as '@chip',
    PB.Image1 as '@image',
    PB.Trade_Price as '@tradeprice',

    (SELECT
        TB.UID as '@tariffid',
        TB.Tariff_Name as '@name',
        TB.Carrier as '@network',
        TB.Inclusive_Minutes as '@mins',
        TB.Inclusive_Texts as '@texts',
        TB.Inclusive_Data as '@data',
        TB.Monthly_Cost as '@monthly',
        TB.Commission as '@comm',
        (TB.Commission - PB.Trade_Price) as '@upfront'

        FROM dbo.Tariff_Base TB
        WHERE TB.Active = 1 AND TB.Type = 1
        FOR XML PATH('tariff'), TYPE

    ),

    (SELECT

        OP.GP_Margin as '@gpmargin'

    FROM dbo.Options OP
    FOR XML PATH('options'), TYPE
    )

    FROM dbo.Phone_Base PB
    WHERE PB.Friendly_URL_Name = @phone AND PB.Active = 1
    FOR XML PATH('detail'), TYPE

END

我想做的是: 在内部选择(TB)是调用另一个SP让我们调用它"计算"传递2个变量(TB.Commission和PB.Trade_Price)作为总和

Calculate将向存储过程返回一个值,即@hp,该存储过程可以在XML List中添加/使用。

可以在SQL Server 2014 / T-SQL中完成吗?

1 个答案:

答案 0 :(得分:1)

没有。但你可以用一个功能来做。请参阅MSDN文档,尤其是示例A.

像这样(未经测试):

CREATE FUNCTION dbo.Calculate (@Comission float, @TradePrice float)  
RETURNS float
WITH EXECUTE AS CALLER  
AS  
BEGIN  
     DECLARE @SumVal float;  
     SET @SumVal = @Commission + @ TradeValue;
     RETURN(@SumVal);  
END;  
GO  
--In your sub-query
SELECT values, dbo.Calculate(TB.Commission, PB.Trade_Price) AS A_Sum
FROM ...;