troubleshooting Question

Incrementing a value in a stored procedure

Avatar of htabbach
htabbachFlag for United States of America asked on
Microsoft SQL Server 2005
17 Comments1 Solution295 ViewsLast Modified:
Hello,
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
      @BillingInterval int,
      @UserID varchar(50),
      @QB_InvoiceNumber int,
      @InvoiceDate datetime
        
AS
BEGIN
      -- 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
END
GO
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 17 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros