(4.17)sql server中的uuid获取与使用
sql server中的uuid
建表:
1、自增长
studentno int primary key identity(1,1)——bigint也是可以的
2、创建uuid
customerid uniqueidentifier primary key default newid()
获取UUID:
select newid() 即可
1.获取32位的UUID -----批处理
declare @newid varchar(36)=newid()
select SUBSTRING(@newid,1,8)+SUBSTRING(@newid,10,4)+SUBSTRING(@newid,15,4)+ SUBSTRING(@newid,20,4)+SUBSTRING(@newid,25,12)
2.获取32位的UUID -----函数
CREATE FUNCTION [dbo].[FunGetUUID32](@newid varchar(36)) RETURNS VARCHAR(32) AS BEGIN DECLARE @id VARCHAR(32); select @id=SUBSTRING(@newid,1,8)+SUBSTRING(@newid,10,4)+SUBSTRING(@newid,15,4)+ SUBSTRING(@newid,20,4)+SUBSTRING(@newid,25,12) RETURN @id END --使用如下 select dbo.[FunGetUUID32](NEWID());
3.直接替换
select replace(newid(),'-','');