ITPub博客

首页 > 数据库 > SQL Server > 生成1千万个随机串号9位英文字母

生成1千万个随机串号9位英文字母

原创 SQL Server 作者:wghao 时间:2007-06-15 13:16:36 0 删除 编辑
今天上CSDN社区的时候,发现这帖子,感觉顶有意思,就想一下写出一解决方法。这里顺便记录自己的解决方法。[@more@]原来问题:
请问一下,在SQL中怎么来做可以快速生成1千万个9位英文(大写)随机不重复的号码,非常着急,谢谢啦。

自己写的一个解决方法:
DECLARE @i int CREATE TABLE #AZ(X nchar(1))SET @i=65WHILE @i<=90BEGININSERT INTO #AZ SELECT CHAR(@i)SET @i=@i+1ENDSELECT TOP 10000000
A.X
+B.X+C.X+D.X+E.X+F.X+G.X+.H.X+I.X AS X INTO #T1FROM
(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()DESC) AS A
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID())AS B
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()DESC) AS C
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()) AS D
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()DESC) AS E
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()) AS F
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()DESC) AS G
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID()) AS H
,(
SELECT TOP 32 X FROM #AZ ORDER BY NEWID() DESC) AS IWHERE A.X<>B.X AND A.X<>C.X AND A.X<>D.X AND A.X<>E.X AND A.X<>F.X AND A.X<>G.X AND A.X<>H.X AND A.X<>I.X AND B.X<>C.X AND B.X<>D.X AND B.X<>E.X AND B.X<>F.X AND B.X<>G.X AND B.X<>H.X AND B.X<>I.X AND C.X<>D.X AND C.X<>E.X AND C.X<>F.X AND C.X<>G.X AND C.X<>H.X AND C.X<>I.XAND D.X<>E.X AND D.X<>F.X AND D.X<>G.X AND D.X<>H.X AND D.X<>I.XAND E.X<>F.X AND E.X<>G.X AND E.X<>H.X AND E.X<>I.XAND F.X<>G.X AND F.X<>H.X AND F.X<>I.XAND G.X<>H.X AND G.X<>I.XAND H.X<>I.X--执行使用 1分20秒PRINT GETDATE()--查询数据未测,因为电脑慢,呵呵
--
SELECT * FROM #T1PRINT GETDATE()DROP TABLE #AZ,#T1

要是哪位朋友想到更好的方法可以写出,大家一起分享!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7764484/viewspace-918965/,如需转载,请注明出处,否则将追究法律责任。

下一篇: Linux学习笔记1
请登录后发表评论 登录
全部评论

注册时间:2007-12-19

  • 博文量
    57
  • 访问量
    233003