2
Answers

auto_increment code stored procedures sql server

Mayoorann Nava

Mayoorann Nava

8y
285
1
this code work but auto_increment not work what is the right code.. correct me
 
ALTER PROCEDURE [dbo].[GRNInfo]
@GrnCode nchar(10) ,
@ItemCode nchar(10) ,
@Qty int ,
@TotalPrice decimal(18, 0) ,
@GrnDate date
AS
BEGIN
DECLARE @Max INT
if not exists (select GrnCode from atoGRN where GrnCode=@GrnCode)
begin
 ------------------------------------------------------------------------------------------------------------------
--SET @GrnCode = 'GRN00001'
SELECT @Max = CONVERT(INT, SUBSTRING(CONVERT(VARCHAR(10),@GrnCode), 3, 10)++1) FROM atoGRN
SET @GrnCode = 'GRN' + RIGHT('0000' + CONVERT(VARCHAR(10),@Max ++ 1 ) , 5)
 
---------------------------------------------------------------------------------------------------------- 
insert into atoGRN (GrnCode,FK_ItemCode,Qty,TotalPrice,GrnDate) values (@GrnCode,@ItemCode,@Qty,@TotalPrice,@GrnDate)
end

Answers (2)