Click here to Skip to main content
15,915,703 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi


Doid I am getting by counting number of records in database.
My requirement is if it is a new year the count must start again from 1
How do I automattically reset a sequence value to 1 in every year starts

Example(2011)

2011scl001-D0
2011scl002-DO
2011scl003_Do

(2012)

2012scl001-D0
2012scl002-DO
2012scl003_Do

Thanks
Posted

See this and modify as per your table stucture

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--DELETE FROM Scl_info 
--SELECT * FROM Scl_info 
--EXEC [sp_SaveSclinfo] '2012scl002','GHI'
CREATE PROCEDURE [dbo].[sp_SaveSclinfo]
 (
	@SclId NVARCHAR(50) ,
	@SclName NVARCHAR(50) 
 )
 AS 
 BEGIN
  DECLARE @Scl_Id NVARCHAR(50)
  DECLARE @SclNumber Bigint	
  
  If EXISTS (SELECT * FROM Scl_info WHERE SclId=@SclId)
   Begin
       Update Scl_info
        SET 					
			SclName=@SclName 
       WHERE SclId=@SclId
   End
  Else
   Begin
		SELECT @SclNumber =(COALESCE(MAX(SclNumber),0) +1) FROM Scl_info WHERE SclYear=Year(GetUtcDate())

		SELECT @SclId=CAST(Year(GetUtcDate()) AS NVARCHAR(50)) + 'scl00' +  CAST(@SclNumber AS NVARCHAR(50)) + '-D0' 

		INSERT INTO Scl_info SELECT @SclId,@SclNumber,Year(GetUtcDate()),@SclName
   End		
END


Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
 
Share this answer
 
Comments
Lancy.net 12-Feb-12 4:30am    
Thanks...i will try and revert u soon..
Lancy.net 12-Feb-12 4:55am    
Hi Rahul i am new to this..
would you mind in explaining this to me...
Thanks
If you have three separate data lets say You have
1. CurrentDate
2. CustID
3. CurrentMonth

Then,
C#
DoId = getDoId(YearofCurrentDate, CustId, CurrentMonth);

Call the getDoId Method
C#
private string getDoId(String YYYY, String C_Id, String Month)
{
    String ID = "";
    ID = YYYY + C_Id + ("00") + Month;
    return ID;
}

So Passing the correct year, custid, month will give you the desired output

Hope this will help you
 
Share this answer
 

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