0

I want to Insert The data in to the stored procedure.There is condition that I want to satisfy.Select Max value from OF Table Field "OurRef" From Table "tblPurchaseInvoice" Where SupplierID=SomeID.Then If OurRef>0 Then Value to be inserted is OurRef+1 Else Set Vale OurRef=100.I have written Stored Procedure But It is not working.Thanks in Advance.Below

ALTER PROCEDURE [dbo].[addPurchaseInvoicing]
(
 @JobID int = null,
 @InvoiceDate datetime,
 @SupplierID int,
 @SuppliersInvoiceNumber VARCHAR(40),
 @PurchaseOrderRef varchar(20),
 @Type tinyint,
 @Paid tinyint,
 @ReferencePurchaseInvoiceID tinyint = null
)
AS
DECLARE @ID INT = 0;
DECLARE @OURREF AS INT=0;
BEGIN
 IF  ((select MAX(OurRef) from tblPurchaseInvoice where SupplierID=@ID)=NULL)
 BEGIN
  SET @OURREF='100'
 END
 ELSE IF(@OURREF>'0')
  BEGIN
   SET @OURREF='100'+'1'
  END
BEGIN 
    INSERT INTO tblPurchaseInvoice(
        JobID,
        InvoiceDate,
        SupplierID, 
        OurRef,
        SuppliersInvoiceNumber,
        PurchaseOrderRef,   
        [Type],     
        Paid,
        ReferencePurchaseInvoiceID,
        OurRef
       )
VALUES (
        @JobID,
        @InvoiceDate,
        @SupplierID,
        @OurRef,
        @SuppliersInvoiceNumber,
        @PurchaseOrderRef,
        @Type,
        @Paid,
        @ReferencePurchaseInvoiceID,
        @OURREF
 );
 SET @ID = @@IDENTITY;
END
RETURN @ID
END
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Csharp
  • 177
  • 4
  • 15

1 Answers1

0

Not sure if this is the main reason its not working, but using (Value = NULL) instead of (Value IS NULL) could cause some problems

((select MAX(OurRef) from tblPurchaseInvoice where SupplierID=@ID)=NULL) 

could be rewritten as:

((select MAX(OurRef) from tblPurchaseInvoice where SupplierID=@ID) IS NULL)

= NULL vs. IS NULL

Community
  • 1
  • 1
nbilello
  • 1
  • 1