【问题标题】:Get the value of a stored procedure inside another stored procedure with select使用 select 获取另一个存储过程中的存储过程的值
【发布时间】:2017-02-14 18:13:12
【问题描述】:

如你所见,我有这个存储过程

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

如您所见,此存储过程接受输入并返回一些值。我想用select statement 从另一个存储过程中调用这个存储过程来获得这样的值:

CREATE PROCEDURE secondSP
AS
    declare @a nvarchar(max)
BEGIN
    select @a = shortage 
    from SPViewMTO(1)    // The input value @PackId is 1 for example
END

【问题讨论】:

  • 程序不能像你一样使用。改用表值函数
  • @AksheyBhat 可以在表格函数中调用存储过程吗?
  • Yoг 正在改变另一个程序中的程序??不,您不能在函数内部使用 SP。
  • @EhsanAkbar 只需将您的选择查询放在表值函数中
  • 如果你在shortage 中有超过 1 个不同的值怎么办?您将只传递给@a 第一个

标签: sql-server select stored-procedures


【解决方案1】:

创建用于获取数据包信息的表值函数:

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

修改你的第二个过程如下:

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

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

    END

【讨论】:

    猜你喜欢
    • 2011-04-26
    • 2018-03-04
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多