学海无涯,回头是岸
不务正业
极简老人

存储过程:

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

可做表唯一标识。

posted on 2017-06-16 15:55  MirageFireFox  阅读(1667)  评论(2编辑  收藏  举报