I think there is an easier way to get this done with a CTE, but I can't seem to get it right. I'm trying to return the next invoice number from this table and also increment the invoice number in one procedure. I would also like to return the editcount column. My problem with the CTE was once i used it in the SELECT @NextInvoiceNbr, then I couldn't use it in the update. Can somebody give some advice as to how I can accomplish this, with or without a CTE? Here's the current proc.
Auguy
Sylvania/Toledo Ohio
Code:
--Parameters
@CompanyMasterFK int,
@NextInvoiceNbr int output,
@MyError int output
AS
BEGIN SELECT @NextInvoiceNbr = (Select NextNumber From dbo.CompanyInvoiceNumber Where CompanyMasterFK = @CompanyMasterFK)
SELECT @MyError = @@ERROR
IF @MyError = 0
BEGIN
Update dbo.CompanyInvoiceNumber
Set NextNumber = @NextInvoiceNbr + 1,
EditCount = EditCount + 1
Where CompanyMasterFK = @CompanyMasterFK And NextNumber = @NextInvoiceNbr
SELECT @MyError = @@ERROR
END
END
Auguy
Sylvania/Toledo Ohio