0
我想將數據插入存儲過程。有條件需要滿足。 Select Max value from OF Table Field "OurRef" From Table "tblPurchaseInvoice" Where SupplierID=SomeID.Then If OurRef>0
然後值將插入是OurRef+1
Else Set Vale OurRef=100
。我已經寫存儲過程,但它不是working.Thanks在Advance.Below從表中選擇最大值並在存儲過程中使用If Else條件設置值
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 [email protected])=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