sql生成随机不重复字符串 可指定长度

存储过程:

create procedure dbo.GetRandStr
(@Count int,@no varchar(8) output)
AS
BEGIN
DECLARE @RANDOMSTR VARCHAR(100), @CHARPOOL VARCHAR(36), @ss varchar
DECLARE @I INTEGER, @counter INTEGER
SET @CHARPOOL = '0123456789abcdefghijklmnopqrstuvwxyz'
SET @I = 1
SET @RANDOMSTR = ''
WHILE @I <= @Count
BEGIN
HERE:
SET @counter = CAST(RAND() * 100 / 2.75 AS INTEGER)
IF @counter < 1 GOTO HERE
SET @RANDOMSTR = @RANDOMSTR + SUBSTRING(@CHARPOOL, @counter ,1)
SET @I = @I + 1
END
set @no = left(@RANDOMSTR,isnull(@Count,8))
return
END
GO

执行存储过程:

DECLARE @something VARCHAR(12)
exec GetRandStr 8,@something output
PRINT @something
GO

可做表唯一标识。

上一篇:canvas实现刮刮卡效果


下一篇:51Nod1166 大数开平方