想通过一个函数或sql语句,随机生成任意6个字母组成的字符!
如题!
------解决方案-------------------- declare @sql nvarchar(400)
select @sql= 'select char( '+cONVERT(NVARCHAR,CONVERT(INT,26*rand())+97)+ ')+
CHAR( '+CONVERT(NVARCHAR,CONVERT(INT,rand()*26)+97)+ ')+
CHAR( '+CONVERT(NVARCHAR,CONVERT(INT,rand()*26)+97)+ ')+
CHAR( '+CONVERT(NVARCHAR,CONVERT(INT,rand()*26)+97)+ ')+
CHAR( '+CONVERT(NVARCHAR,CONVERT(INT,rand()*26)+97)+ ')+
CHAR( '+CONVERT(NVARCHAR,CONVERT(INT,rand()*26)+97)+ ') '
print @sql
EXEC(@sql)
------解决方案--------------------declare @str varchar(10)
set @str = ' '
select @str = @str + chr from
(select top 6 chr from (
select 'a ' as chr union all
select 'b ' union all
select 'c ' union all
select 'd ' union all
select 'e ' union all
select 'f ' union all
select 'g ' union all
select 'h ' union all
select 'i ' union all
select 'j ' union all
select 'k ' union all
select 'l ' union all
select 'm ' union all
select 'n ' union all
select 'o ' union all
select 'p ' union all
select 'q ' union all
select 'r ' union all
select 's ' union all
select 't ' union all
select 'u ' union all
select 'v ' union all
select 'w ' union all
select 'x ' union all
select 'y ' union all
select 'z '
) as a ORDER BY NEWID()) as t
----查看
select @str
------解决方案--------------------DECLARE @myid varchar(1000)
SET @myid = NEWID()
select CONVERT(varchar(6), @myid)
------解决方案--------------------DECLARE @Below int
DECLARE @Up int
SELECT @Below=65,@Up=90
SELECT CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
+CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
+CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
+CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
+CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
+CHAR(CAST(RAND()*(@Up-@Below)+@Below AS decimal(38,0)))
------解决方案-------------------- CREATE VIEW V_RAND AS SELECT RAND1 = CONVERT(INT,RAND()*26),RAND2 = RAND()*2
GO
CREATE FUNCTION DBO.f_GetRandNum(@LEN INT,@FLAG INT)
RETURNS NVARCHAR(100)
AS
--@LEN 输出字符的长度
--@FLAG 返回值包含字符 1:大写字母 2:小写字母 3:大小写字母混合
BEGIN
DECLARE @SQL NVARCHAR(100),@RAND INT
SELECT @SQL = ' '
IF @LEN > 100
SET @LEN = 100
WHILE @LEN > 0
BEGIN
SELECT @RAND = RAND1 + (CASE @FLAG WHEN 1 THEN 65 WHEN 2 THEN 97
ELSE (CASE WHEN RAND2 > 1 THEN 97 ELSE 65 END) END)
FROM V_RAND
SELECT @SQL = @SQL + CHAR(@RAND),@LEN = @LEN - 1
END
RETURN @SQL
END
GO
SELECT DBO.f_GetRandNum(7,3)