sql随机生成字符串
① sql 单个字符串位置随机
DECLARE@textVARCHAR(MAX)='abcdefghijklmn'
DECLARE@indexINT=0
DECLARE@lenINT=0
DECLARE@resultVARCHAR(MAX)=''
SET@len=LEN(@text)
DECLARE@tbTABLE
(
textVARCHAR(10),
randnoINT
)
WHILE(@index<LEN(@text))
BEGIN
INSERTINTO@tb
(
text,
randno
)
VALUES
(
LEFT(@text,1),
RAND()*@len
)
SET@text=RIGHT(@text,LEN(@text)-1)
END
SELECT@result+=textFROM@tb
ORDERBYrandno
PRINT@result
将就看下吧,你直接拿到数据库执行看结果
② PLSQL编程:如何自动向数据库中插入随机字符串
每次插入前检查一下,因为如果是随机的,即使概率很小还是有可能重复的。要不有个简单的方法,一个比一个长,字数不同
③ 用SQL语言随机生成规定范围内的汉字
mssqlserver:
select top 1 * from (
select '男' union all
select ‘女’)T
order by newid()
oracle:
with T as(
select '男' from al union all
select '女' from al)
select * from (select * from T order by sys_guid()) --order by在外层的话不起作用
where rownum=1
④ 用sql如何随机生成字符串
1.利用newid()产生的uniqueidentifier都是随机且唯一的;
示例:
declare @string nvarchar(100);
set @string =cast(newid() as nvarchar(100));
select @string;
go
2
2.利用rand()生成随机数字串;
示例:
declare @string nvarchar(100);
set @string = right(str(rand(),8,6),2);
select @string;
go
3
3.利用rand()生成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)
go
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)))
go
SELECT CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+
CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+
CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+
CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+
CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+
CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))
go
4
4.编写可以产生随机字符串的存储过程;
示例:
--*********************
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
RETURN @SQL
GO
--调用存储过程
SELECT DBO.f_GetRandNum(7,2)
⑤ 用SQL怎么写一个生成随机的六位数
用SQL写一个生成随机的六位数代码如下:
declare @i int
set @i=0
while @i<1073
begin
update Actor set AtrPwd = RIGHT(100000000 + CONVERT(bigint,
ABS(CHECKSUM(NEWID()))), 6) --where aid=2
set @i=@i+1
end
⑥ 怎样用SQL向数据库中批量的插入数据,主键是随机生成的
代码如下:
--创建测试表
CREATETABLE[Identity](
IdINTIDENTITY(1,2)NOTNULLPRIMARYKEY,--种子的起始值1,步长2
NumberVARCHAR(20)UNIQUENOTNULL,
NameVARCHAR(20)NOTNULL,
PasswordVARCHAR(20)DEFAULT(123),
DescriptionVARCHAR(40)NULL
)
--插入记录
INSERTINTO[Identity](Number,Name,Description)VALUES('001','1st','Id=1,因为起始值1')
INSERTINTO[Identity](Number,Name,Description)VALUES('002','2nd','Id=3,因为起始值1,步长2')
INSERTINTO[Identity](Number,Name,Description)VALUES('003','3rd','Id=5,由于字符长度超长,报错插入失败,造成此Id产生后被放弃')
INSERTINTO[Identity](Number,Name,Description)VALUES('004','4th','Id=7not5,因为第三条记录插入失败')
--检索记录,查看结果
SELECT*FROM[Identity]
⑦ SQL语言随机生成字符串的几种方法
1.利用newid()产生的uniqueidentifier都是随机且唯一的:declare @string nvarchar(100);set @string =cast(newid() as nvarchar(100));select @string;go2.利用rand()生成随机数字串:declare @string nvarchar(100);set @string = right(str(rand(),8,6),2);select @string;go3.利用rand()生成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) goDECLARE @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))) goSELECT CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+ CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+ CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+ CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+ CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END))+ CHAR(CONVERT(INT,rand()*26)+(CASE WHEN RAND()*2 > 1 THEN 97 ELSE 65 END)) go4.编写可以产生随机字符串的存储过程:--*********************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,2) (收集自:CSDN社区)