0

In the below code i have a function and a query the function will return batchrelease quantity.I have include the function in the select sqlquery it throws a error"Procedure or function dbo.GetBatchReleaseQuantity has too many arguments specified.".Please help me to overcome this issue.

SELECT   p.ProductID,
   p.ProductName,
   ISNULL((SELECT ISNULL( CurrentStock,0.00) 
    FROM   Productstatus PS
    WHERE  PS.ProductID =p.ProductID
           AND PS.LocationID = 1
           AND PS.StatusDateTime= '2014-08-27'
           and PS.productid=p.productid),0) OpeningStockQuantity,
          ISNULL((SELECT ISNULL( (CurrentStock*PS.UnitPrice),0.00) 
    FROM   Productstatus PS
    WHERE  PS.ProductID =p.ProductID
           AND PS.LocationID = 1
           AND PS.StatusDateTime= '2014-08-27'
           and PS.productid=p.productid),0) OpeningStockValue,
           ISNULL((SELECT ISNULL( CurrentStock,0.00) 
    FROM   Productstatus PS
    WHERE  PS.ProductID =p.ProductID
           AND PS.LocationID = 1
           AND PS.StatusDateTime= '2014-08-27'
           and PS.productid=p.productid),0) ClosingStockQuantity,
          ISNULL((SELECT ISNULL( (CurrentStock*PS.UnitPrice),0.00) 
    FROM   Productstatus PS
    WHERE  PS.ProductID =p.ProductID
           AND PS.LocationID = 1
           AND PS.StatusDateTime= '2014-08-27'
           and PS.productid=p.productid),0) ClosingStockValue,
           (SELECT dbo.GetBatchReleaseQuantity(1,2, '2014-01-27 00:00:00', '2014-11-27 23:59:59')  AS BatchReleaseQuantity
        FROM   Productstatus PS   
        WHERE    
                PS.LocationID = 1 
              and PS.productid=p.productid AND PS.StatusDateTime > = '2014-01-27 00:00:00' AND PS.StatusDateTime <= '2014-10-27 23:59:59' ) 

    --        ISNULL((SELECT ISNULL( (dbo.GetBatchReleaseQuantity(1,2, '2014-01-27 00:00:00', '2014-11-27 23:59:59')),0.00) 
    --FROM   Productstatus PS
    --WHERE  PS.ProductID =p.ProductID
    --       AND PS.LocationID = 1
    --       AND PS.StatusDateTime= '2014-08-27'
    --       and PS.productid=p.productid),0) Batchout

           FROM   Product P
   --- SELECT dbo.GetBatchReleaseQuantity(@i_LocationID,P.ProductID,@i_Date,@i_Date) From Product P      




LEFT OUTER JOIN LocationProductMap LMP ON LMP.ProductID=P.ProductID
WHERE LMP.ProductInFlow=1

Function

ALTER FUNCTION [dbo].[GetBatchReleaseQuantity]   
()  
RETURNS int  
 --WITH ENCRYPTION     
AS  
BEGIN  

 DECLARE @i_LocationID VARCHAR(50)  
 DECLARE @i_ProductID INT  
 DECLARE @i_StartDate VARCHAR(50)  
 DECLARE @i_EndDate VARCHAR(50)  


  RETURN (SElECT SUM( BatchReleaseQuantity) AS BatchReleaseQuantity From  BatchReleaseDetails BRD
   LEFT OUTER JOIN BatchRelease BR ON BR.BatchReleaseID=BRD.BatchReleaseID
    Where  ProductId=@i_ProductID  AND LocationID=@i_LocationID AND BRD.CreatedOn>=@i_StartDate AND BRD.CreatedOn<=@i_EndDate)
1
  • Looks to me like the variable decs in the function are in the wrong place. Commented Nov 4, 2014 at 9:36

1 Answer 1

4

Your function doesn't have any parameters, you need to put them in the (), for example:

CREATE FUNCTION Add
(
    @P1 INT,
    @P2 INT
)
RETURNS INT
AS
BEGIN
    RETUTN @P1 + @P2
END

So change yours to something like this by moving the variables to be parameters instead:

ALTER FUNCTION [dbo].[GetBatchReleaseQuantity]   
(
    @i_LocationID VARCHAR(50),
    @i_ProductID INT,
    @i_StartDate VARCHAR(50),  
    @i_EndDate VARCHAR(50)  
)  
RETURNS int  
 --WITH ENCRYPTION     
AS  
BEGIN  
  RETURN (SELECT SUM( BatchReleaseQuantity) AS BatchReleaseQuantity From  BatchReleaseDetails BRD
   LEFT OUTER JOIN BatchRelease BR ON BR.BatchReleaseID=BRD.BatchReleaseID
    Where  ProductId=@i_ProductID  AND LocationID=@i_LocationID AND BRD.CreatedOn>=@i_StartDate AND BRD.CreatedOn<=@i_EndDate)
END

Also you probably need to think about how you are calling it as you appear to be passing in an INT as the first parameter but in the function it's declared as a VARCHAR(50). So it may need to be that your @i_LocationID parameter should be an INT.

Sign up to request clarification or add additional context in comments.

2 Comments

when i execute the above query it throws subquery return more than one value
That's a problem with your stored procedure and is another problem completely. Deconstruct the query into parts that work and join them together one by one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.