Getting a value from stored procedure within a stored procedure

╄→гoц情女王★ 提交于 2019-12-12 01:42:53

问题


I have a stored procedure which returns an XML file. At the moment some calculations are done in XSL but I would like to do these within the database using another stored procedure. (adding the result of that calculation to the 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

What I want to do is: In the inner select (TB) is to call another SP lets call it "calculate" passing 2 variables (TB.Commission and PB.Trade_Price) for the sum

Calculate will return a value i.e. @hp to the stored procedure which can be added/used in the XML List.

Can this be done in SQL Server 2014/T-SQL?


回答1:


No. But you could do it with a function. See the MSDN documentation, especially example A.

Something like this (untested):

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 ...;  


来源:https://stackoverflow.com/questions/39140672/getting-a-value-from-stored-procedure-within-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!