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社區)