Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table t_customer_account where 1000 records are there.

I want to make an account no.
My format is
01010234000001
here 01 if financial year id
01 is company id
o2 is branch id
34 is scheme id
and rest 6 digit is slno
I want to make the slno as branch and scheme wise.
In my table all the fields are present like
Financial_id,Branch_id,Company_id,Scheme_Id

I want to create a procedure and created also but it is not fetch all the 14 digit number

my query is:-

////////
SQL
alter procedure [dbo].[generate_acno]
as
begin
	DECLARE @fnno varchar(2)
	DECLARE @cmpid VARCHAR(2)
	DECLARE @brid int
	DECLARE @scid varchar(2)
	DECLARE @Cusid int
    declare @accno varchar(6)
	declare @sql varchar(100)
	declare @slno int
	declare @StrCode varchar(8)
	set @fnno='01'
	set @cmpid='01'
	
	--SET @CNT=0
	DECLARE VoucherCursor CURSOR  
	FOR
	SELECT customer_id,Branch_id,cast(Scheme_id as varchar(2)) FROM t_customer_account order by customer_id
    OPEN VoucherCursor;
    FETCH NEXT FROM VoucherCursor INTO @Cusid,@brid,@scid;
	--update t_voucher_master set voucher_no=voucher_no+'*'
   WHILE @@FETCH_STATUS = 0
   BEGIN
	select @scid;
	if len(@brid)=1 
			set @StrCode=@fnno+@cmpid+'0'+cast(@brid as varchar(1))+@scid 
	else
		set @StrCode=@fnno+@cmpid+cast(@brid as varchar(1))+@scid 
	select @StrCode;
	   set @accno=(select +@StrCode+REPLICATE('0',6-LEN(CONVERT(INT,(isnull(max(substring(passbook_No,9,6)),'000000')))))+ CONVERT(VARCHAR,CONVERT(INT,(isnull(max(substring(passbook_No,9,6)),'000000'))+1)) from T_Customer_Account where passbook_No like +''''+@StrCode+'%'+'''');
       select @accno;
		--select ''' + @StrCode + ''' + REPLICATE('0',6-LEN(CONVERT(INT,(SUBSTRING(isnull(max(Account_No),'000001'),9,6)))+1)) + CONVERT(VARCHAR,CONVERT(INT,(SUBSTRING(isnull(max(Account_No),'000001'),9,6))+1)) from T_Customer_Account where Account_No like +''' + StrCode + ''%'' +''';
		--set @CNT=@CNT+1;
		--SET @VOUCHER_NO='Pv'+cast(@CNT as varchar);
		update t_customer_account set passbook_no=@accno where customer_id=@Cusid;
		FETCH NEXT FROM VoucherCursor INTO @Cusid,@brid,@scid;
   END;
end;
CLOSE VoucherCursor;
DEALLOCATE VoucherCursor;
//////

please help me.
thanks.
Posted
Updated 14-Nov-11 19:16pm
v2

1 solution

Hello,


First of all i would say that you did a big mistake while creating table design for this. You should have added one column which stores the above Code in given format .Now you have to write this logic all over the database where you want to access "account no" which is a tiresome job. If you simple add one column in table "t_customer_account ". Still its not late to do so.Add one column and write a procedure which will update each record and will create Account No and update it back to record. And modify the logic to insert record for this code.

Do you have any idea how much Processing speed you are wasting on the cost of Memory?


Another thing, Use
RIGHT ('000'+ CAST ( CompanyCode AS varchar), 3) AS CompanyCode
This will automatically insert 0's in right if Code is like 99 it will be 099 afterwards. You don't need to check length and have to write logic for this.

Could you please send your table structure here
 
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