DBA2010
asked on
SQL Code
I have a SP check the invoice numbers in the invoice table and return the next available number.
but I think thats not working good because I have a douplicate record.
so what I need to do I want to create a SP to check the number I get from the system SP and Keep it in a LOOP until it find its a uniqe number then return it to me.
any Ideas?
but I think thats not working good because I have a douplicate record.
so what I need to do I want to create a SP to check the number I get from the system SP and Keep it in a LOOP until it find its a uniqe number then return it to me.
any Ideas?
ASKER
The invoice is not an IDENTITY not a uniqe
one way is try using an autonumber, intead of doing it on sp,
also you can try is create another table of invoices numbers something like this
invoice number status
1 close
2 close
3 open
4 free
every time you do a max(invoice number) + 1 where status is free if there is not insert a new one on the table, if the invoice is complete susscecfolly mark it as close, if not free the record, with this way, you won't lose numbers and will not going to have integrity problems.
hope some of this helps you
regards
also you can try is create another table of invoices numbers something like this
invoice number status
1 close
2 close
3 open
4 free
every time you do a max(invoice number) + 1 where status is free if there is not insert a new one on the table, if the invoice is complete susscecfolly mark it as close, if not free the record, with this way, you won't lose numbers and will not going to have integrity problems.
hope some of this helps you
regards
Identity is an auto incremented field and is therefore unique. What I have posted is a method for generating consective unique numbers which is the usual requirement for invoice numbers
ASKER
I have this code but still duplicate the numbercan you help with this?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[invoiceno]
as
declare @invoiceno varchar(24)
execute dbo.getinvoiceNumber 'invoiceNo', @OrderNo Output
select @invoiceNo
WHILE (SELECT invoiceno from dbo.InvoiceTable where invoiceno = @invoiceNo) is not null
BEGIN
if (SELECT invoiceno from dbo.InvoiceTable where invoiceno = @InvoiceNo) is null
BREAK
ELSE
execute dbo.getInvoiceNumber 'invoiceNo', @invoiceNo Output
CONTINUE
select @invoiceNo
END
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[invoiceno]
as
declare @invoiceno varchar(24)
execute dbo.getinvoiceNumber 'invoiceNo', @OrderNo Output
select @invoiceNo
WHILE (SELECT invoiceno from dbo.InvoiceTable where invoiceno = @invoiceNo) is not null
BEGIN
if (SELECT invoiceno from dbo.InvoiceTable where invoiceno = @InvoiceNo) is null
BREAK
ELSE
execute dbo.getInvoiceNumber 'invoiceNo', @invoiceNo Output
CONTINUE
select @invoiceNo
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes
>> Yes ?
Are you referring my post? Did you try that?
Are you referring my post? Did you try that?
The stored procedure gets the next available number and thus ensures uniqueness.
See code
Open in new window