Md Ahsanul Kabir
Md Ahsanul Kabir

Reputation: 83

How can I manually increment an uniqueidentifier field like integer in SQL Server?

declare @id uniqueidentifier
set @id = '0D20697B-1D3C-4440-9BD1-00158D54B690'
set @id = @id + 1
select @id

Error Message :

The data types uniqueidentifier and varchar are incompatible in the add operator.

I want it to work. How?

Upvotes: 2

Views: 395

Answers (1)

Md Ahsanul Kabir
Md Ahsanul Kabir

Reputation: 83

DECLARE  @ID                      VARCHAR(MAX),
        @Hex                     VARBINARY(16),
        @hexString               VARCHAR(MAX)
SET @ID = 'AAA6F580-98C8-42D0-8ACA-21C87589B1F7' --newid

SET @hexString = '0x'+RIGHT(@ID,4)
SET @ID = Left(@ID,32) + RIGHT(UPPER(master.dbo.fn_varbintohexstr (CONVERT(VARBINARY(16), CONVERT(INT, (select cast('' as xml).value('xs:hexBinary( substring(sql:variable("@hexString"), sql:column("t.pos")) )', 'varbinary(max)')
from (select case substring(@hexString, 1, 2) when '0x' then 3 else 0 end) as t(pos))) + 1))),4)

select @ID

Upvotes: 1

Related Questions