如何在SQL Server中手动增加uniqueidentifier字段(如整数)?

时间:2016-03-03 09:59:24

标签: sql-server

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

错误消息:

  

数据类型uniqueidentifier和varchar在中是不兼容的   添加运营商。

我想让它发挥作用。怎么样?

1 个答案:

答案 0 :(得分:0)

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