I have a stored procedure to insert data in a pattern,
DECLARE @I INT = 1
DECLARE @ID INT = 0
DECLARE @PackID VARCHAR(50)
SELECT @PackID = MAX(PackID)
FROM tblPacks
WHERE PackID LIKE 'PK%'
IF(@String = 'Packs')
BEGIN
WHILE @I <= @Count
BEGIN
IF @PackID IS NULL
BEGIN
SET @I = @I + 1
SET @ID = @ID + 1
INSERT INTO tblPacks (T_ID, BatchNumber, PackID, Status, BlistersCount)
VALUES (@T_ID, @BatchNumber,
'PK'+ CAST(@ID as VARCHAR(50)), 0,
(SELECT NumberOfBlistersInEachPack
FROM tblPackDetails
WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber))
END
ELSE
BEGIN
SET @I = @I + 1
error :- SELECT @ID = (SELECT MAX(PackID) FROM tblPacks)
SET @ID = @ID + 1
INSERT INTO tblPacks (T_ID, BatchNumber, PackID, Status, BlistersCount)
VALUES (@T_ID, @BatchNumber,
'PK'+ CAST(@ID as VARCHAR(50)), 0,
(SELECT NumberOfBlistersInEachPack
FROM tblPackDetails
WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber))
END
END
END
END
It is showing an error:
Conversion failed when converting the varchar value 'PK3' to data type int.
If packid is null then it Should be 'PK1' and if it has some data then it will take max value and then it should be incremented by'1'. Where should I modify my code to get rid of this error?
tabletblPacks what is thedatatypeofcolumnPackID ? - Abhishek