Click here to Skip to main content
14,928,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. I have to insert an Orders form where I have to insert fields like Order_Num, Order_Date, Product_ID, Order_Amount, etc... My problem is that the Order_Num (which is the record ID) is preformatted like this: <P1109-0001> where:
"P" constant character (PurchaseOrder); "11" is the current year '2011'; "09" is the current month (Sept); "-" is constant character (separator); "0001" is the series number.
This series number increments whenever a new record is saved to the SQL table "Orders table".

In VB6, I am able to loop through the table to search for a duplicate ID and when there is no duplicate, the record with that ID is inserted.

Can you please guide me on how to do it in I am not also familiar with stored_proc in SQL so I usually code using VB.

Thanks in advance.

You should have another table in the database that stores your series number. Then you can just retrieve this number whenever you want to insert a new purchase order, create your unique id, increment the series number and save all to database.

Hope this helps
store procedure takes the tablename, columnname as input parameter and retruns the next number
Employee table will return output as E11090001

create proc GetNextNumber
@tableName nvarchar(100),
@columnName nvarchar(100)
declare @strSql nvarchar(1000)
set @strSql = ' select ''' + upper(right(@tableName,1)) + ''' + 
		left(convert(nvarchar,GETDATE(),12),4) + 
		right(''0000'' + convert(nvarchar,max(right(' +@columnName + ',4))+ 1),4) 
	from ' + @tableName
exec sp_executesql  @strsql
Hi Wayne, the series number resets to zero when the Year or the Month changes. For example, the Orders record <P1108-0326>
(2011-Aug, series# 326)
will be expected to set to P1109-0001 once the date changes to September 01 (2011-Sep-01).
hi sachin10d, I will try this procedure. However, I have to study how to call SQL storedproc in dotNet. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900