if exists(select name from sysobjects where name='xsGetRandChar')
drop proc xsGetRandChar
go
-- =============================================
-- Author: yzb
-- Create date: 20140120
-- Description: 生成一个指定长度的随机字符串,默认为8位。
-- =============================================
create proc xsGetRandChar
@len int = 8, -- 随机字符长度
@strChar nvarchar(12) output -- 定义输出变量。返回值为随机字符串
as
IF @len > 12
set @len = 12;
declare @n int;
declare @strSQL varchar(500);
declare @strTmp varchar(7);
set @strTmp = 'select ';
set @strSQL = @strTmp;
set @n = 0;
while @n < @len
begin
set @strSQL = @strSQL+ char(cast(rand()*10000 as int)%26+65);
set @n = @n + 1;
end
-- 过滤掉select字符串,得到随机字符串
set @strChar = right(@strSQL,len(@strSQL)-len(@strTmp)-1);
--print @strChar;
--return @strChar;
go
--调用存储,使用输出参数获取。
declare @strChar nvarchar(12)
exec xsGetRandChar 9,@strChar output
print @strChar
SQL存储过程生成一个指定长度的随机字符串
最新推荐文章于 2022-01-10 18:20:06 发布