I have a stored procedure that takes a value out of the database (eg. A001UD7) and recursively increments it. If the value is a 9, the next should be A, and if the value is Z the next is 0. Here is the stored procedure:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER PROCEDURE dbo.CREATEKEY (
@table VARCHAR(255))
AS
DECLARE @newKEY AS VARCHAR(32)
DECLARE @I AS INT
DECLARE @COMPARE AS CHAR(1)
/*
**select the last used key
*/
SELECT @newKEY = (SELECT KEYVALUE FROM sysdba.SITEKEYS WHERE KEYDESC=@table AND SITECODE='XXXX')
/*
**increment the key
*/
/*
** Reverse loop through the variable from right to left look at and increment
** characters and numbers.
*/
/*
SET @I = 7
WHILE @I>0
BEGIN
SET @COMPARE = SUBSTRING(@newKEY, @I, 1)
IF ((CAST(@COMPARE AS INT)) < 9 )
BEGIN
SET @COMPARE = @COMPARE + 1
SET @newKEY = @COMPARE SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
SET @I = 0
BREAK
END
ELSE
BEGIN
IF (@COMPARE = 'Z')
BEGIN
SET @COMPARE = 0
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
ELSE
BEGIN
IF ((CAST(@COMPARE AS INT) = 9)
BEGIN
SET @COMPARE = 'A'
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
ELSE
BEGIN
SELECT @COMPARE = CHAR(ASCII(@COMPARE) + 1)
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
END
END
SET @I = @I - 1
END
*/
/*
**write it back
*/
UPDATE sysdba.SITEKEYS
SET KEYVALUE=@newKEY
WHERE KEYDESC=@table AND SITECODE='XXXX'
RETURN @newKEY
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
And here is the error:
Server: Msg 245, Level 16, State 1, Procedure CREATEKEY, Line 74
Syntax error converting the varchar value 'A001UD7' to a column of data type int.
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER PROCEDURE dbo.CREATEKEY (
@table VARCHAR(255))
AS
DECLARE @newKEY AS VARCHAR(32)
DECLARE @I AS INT
DECLARE @COMPARE AS CHAR(1)
/*
**select the last used key
*/
SELECT @newKEY = (SELECT KEYVALUE FROM sysdba.SITEKEYS WHERE KEYDESC=@table AND SITECODE='XXXX')
/*
**increment the key
*/
/*
** Reverse loop through the variable from right to left look at and increment
** characters and numbers.
*/
/*
SET @I = 7
WHILE @I>0
BEGIN
SET @COMPARE = SUBSTRING(@newKEY, @I, 1)
IF ((CAST(@COMPARE AS INT)) < 9 )
BEGIN
SET @COMPARE = @COMPARE + 1
SET @newKEY = @COMPARE SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
SET @I = 0
BREAK
END
ELSE
BEGIN
IF (@COMPARE = 'Z')
BEGIN
SET @COMPARE = 0
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
ELSE
BEGIN
IF ((CAST(@COMPARE AS INT) = 9)
BEGIN
SET @COMPARE = 'A'
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
ELSE
BEGIN
SELECT @COMPARE = CHAR(ASCII(@COMPARE) + 1)
SET @newKEY = SUBSTRING(@newKEY,1,@I-1) + @COMPARE + SUBSTRING(@newKEY,@I+1,7-@I)
END
END
END
SET @I = @I - 1
END
*/
/*
**write it back
*/
UPDATE sysdba.SITEKEYS
SET KEYVALUE=@newKEY
WHERE KEYDESC=@table AND SITECODE='XXXX'
RETURN @newKEY
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
And here is the error:
Server: Msg 245, Level 16, State 1, Procedure CREATEKEY, Line 74
Syntax error converting the varchar value 'A001UD7' to a column of data type int.