Mayooran Navamany

Mayooran Navamany

  • 324
  • 5.6k
  • 133.1k

auto_increment code stored procedures sql server

Jul 21 2016 6:38 AM
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)