Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to Generate Auto Alpha numeric Inv. number in SQL Store procedure
i used Sql Server 2008 r2 and vb.net 2012
I got a error ----

Msg 245, Level 16, State 1, Procedure P_GetAutoIDPurchaseInv, Line 13
Conversion failed when converting the varchar value '17-18/00002' to data type int.


When i just use in this lines then it work fine but when i use full code then show the error please help me sir--
<pre><pre>alter proc P_GetAutoIDPurchaseInv
as
--select top 1 purinvid from tbl_pur_inv order by purinvid desc

begin
if (select top 1 purinvid from tbl_pur_inv order by purinvid desc)=0
declare @lastval char(11) 
set @lastval = '17-18/00001' 
select @lastval 'purinvid'
end


What I have tried:

alter proc P_GetAutoIDPurchaseInv
as
--select top 1 purinvid from tbl_pur_inv order by purinvid desc

begin
if (select top 1 purinvid from tbl_pur_inv order by purinvid desc)=0
declare @lastval char(11) 
set @lastval = '17-18/00001' 

declare @i int 
	set @i = right(@lastval,5) + 1 
	--return 'C' + right('000' + convert(varchar(10),@i),4) 
	return '17-18/'+ right('0000' + convert(varchar(11), @i),5) 
select @lastval 'purinvid'
end
Posted
Updated 28-Sep-17 20:47pm
v3

1 solution

--You can try this

--CREATE A TEMPORARY TABLE
CREATE TABLE #myTemp123
(
                Id INT NOT NULL IDENTITY(1,1),
                AutoId AS 'ABC' + RIGHT('0000'+ CONVERT(VARCHAR(5),Id),5)
)
--INSERT DEFAULT 10000 RECORDS AT ONCE IN LOOP Simply TYPE GO 10000
INSERT INTO #myTemp123 DEFAULT VALUES;
--CHECK INSERTED RECORD
SELECT * FROM #myTemp123
 
Share this answer
 
v2
Comments
Jayanta Modak 29-Sep-17 11:21am    
actually i want to run it two times when inv start time and save time because if other user if save the any inv

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