title: Merge function and stored procedure in SQL Server
body: <p>I have this stored procedure :</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[SPViewMTO] 
  AS
  BEGIN
  
  
  select *,round((Quantity-(QuantityBeforeDone+QuantityExistInSite+QuantitySpool+TotalMIV)),3) As Shortage,CONVERT(nvarchar,CONVERT(int,SheetNumber))+'/'+ CONVERT(nvarchar, CONVERT(int,TotalSheet)) As NewSheetNumber
  
    from(
  SELECT      *
             ,isnull(dbo.WarehouseByMaterialdesciptionId(MaterialDescriptionId),0) As Warehouse
             ,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.ReturnMIVNumberAndQuantity(LineId,TestPackageId,MaterialDescriptionId),0) as MIVs
  
   from dbo.ViewMTO
   )a
  
  END
  </code></pre>
  
  <p>And this function :</p>
  
  <pre><code>    ALTER FUNCTION [dbo].[ReturnMIVNumberAndQuantity](@lineId int,@testpackId int,@matdesId int )
  RETURNS nvarchar(max)
  AS BEGIN
  DECLARE @Names VARCHAR(8000) 
  
  
  SELECT @Names = COALESCE(@Names + ', ', '') + MaterialIssueVouchers.Code+'['+convert(nvarchar,MaterialIssueVoucherDetails.QuantityDeliver)+']'  from (MaterialIssueVouchers inner join
                 MaterialIssueVoucherDetails on 
                 MaterialIssueVouchers.Id=MaterialIssueVoucherDetails.MaterialIssueVoucherId)inner join MaterialRequestContractorDetails on MaterialIssueVoucherDetails.MaterialRequestContractorDetailId=MaterialRequestContractorDetails.Id
                 where MaterialRequestContractorDetails.TestPackageId=@testpackId  and MaterialRequestContractorDetails.LineId=@lineId and MaterialRequestContractorDetails.MaterialDescriptionId=@matdesId
  
      RETURN @Names
  END
  </code></pre>
  
  <p>As you can see this function is called in the stored procedure, because of the speed of query I want to add the function to the stored procedure. </p>
  
  <p>How can I do that?</p>