Incrementing a value in a stored procedure
Posted on 2009-12-20
I need to insert a value in a table so it is incremented by 1 based on a parameter passed to the SP, here is what I did but if I pass @QB_InvoiceNumber as 1 I get 1 in all records, what am I doing wrong?
CREATE PROCEDURE [dbo].[XPSGetInvoiceQueue]
-- Add the parameters for the stored procedure here
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO [360_InvoiceTemp]
(InvoiceID, ClientID, CompanyID, SiteID, InvoiceDate, QB_InvoiceNumber, InvoiceTotal, InvoiceTax, Comments, UserID, Name) SELECT InvoiceID, ClientID, CompanyID, SiteID, InvoiceDate, QB_InvoiceNumber = @QB_InvoiceNumber + 1, InvoiceTotal, InvoiceTax, Comments, @UserID, Name FROM [360_Invoice] WHERE [360_Invoice].Type = 'Template' AND [360_Invoice].InvoiceStatus = 'Active' AND [360_Invoice].BillingInterval = @BillingInterval AND [360_Invoice].InvoiceDate <= @InvoiceDate AND [360_Invoice].InvoiceTotal <> '0.00' ORDER BY [360_Invoice].InvoiceID