Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

sql server - Get the value of a stored procedure inside another stored procedure with select

I have this stored procedure as you can see

create PROCEDURE [dbo].[SPViewMTOByLineIdAndTestPackageId] 
    @PackId int
AS
BEGIN
    SELECT      
        *, 
        ISNULL(dbo.ReturnShortageByItemCodeLinePackage(LineId, TestPackageId, MaterialDescriptionId), 0) AS Shortage,
        ISNULL(dbo.ReturnTotalIMIVByLineIdAndTestPackIdAndMaterialDescriptionId(LineId, TestPackageId, MaterialDescriptionId), 0) AS totalIMIV,
        ISNULL(dbo.ReturnTotalMIVByLineIdAndTestPackIdAndMaterialDescriptionId(LineId, TestPackageId, MaterialDescriptionId), 0) AS TotalMIV,
       ISNULL(dbo.ReturnTotalMRCByLineIdAndTestPackIdAndMaterialDesriptionId(LineId, TestPackageId, MaterialDescriptionId), 0) AS TotalMRC,
       ISNULL(dbo.WarehouseByMaterialdesciptionId(MaterialDescriptionId), 0) AS Warehouse
FROM
    dbo.ViewMTO 
WHERE 
    TestPackageId = @PackId

As you can see, this stored procedure accepts an input and return some values. I want to call this stored procedure from another stored procedure with select statement to get the values like this :

CREATE PROCEDURE secondSP
AS
    declare @a nvarchar(max)
BEGIN
    select @a = shortage 
    from SPViewMTO(1)    // The input value @PackId is 1 for example
END
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

create a table value function for getting packet info:

CREATE FUNCTION dbo.SPViewMTOByLineIdAndTestPackageId(@PackId int)
    RETURNS @packInfo TABLE 
    (
        Shortage int , 
        totalIMIV int , 
        TotalMIV int , 
        JobTitle int , 
        TotalMRC int 
    )
    AS 
    -- Returns the first name, last name, job title, and contact type for the specified contact.
    BEGIN
    SELECT      *,isnull(dbo.ReturnShortageByItemCodeLinePackage(LineId,TestPackageId,MaterialDescriptionId),0) As Shortage
    ,isnull(dbo.ReturnTotalIMIVByLineIdAndTestPackIdAndMaterialDescriptionId(LineId,TestPackageId,MaterialDescriptionId),0) as totalIMIV
    ,isnull(dbo.ReturnTotalMIVByLineIdAndTestPackIdAndMaterialDescriptionId(LineId,TestPackageId,MaterialDescriptionId),0) as TotalMIV
    ,isnull(dbo.ReturnTotalMRCByLineIdAndTestPackIdAndMaterialDesriptionId(LineId,TestPackageId,MaterialDescriptionId),0) as TotalMRC
               ,isnull(dbo.WarehouseByMaterialdesciptionId(MaterialDescriptionId),0) As Warehouse

     from 
    dbo.ViewMTO where  TestPackageId=@PackId
    END

Modify your second proc as below :

   Create PROCEDURE secondSP
    AS
    declare @a nvarchar(max)
    BEGIN

            select @a=shortage from   dbo.SPViewMTOByLineIdAndTestPackageId(1)

    END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...