日期:2014-05-17 浏览次数:20434 次
DECLARE @RANDID INT
SET @RANDID = 0
WHILE LEN(@RANDID)<>6
BEGIN
SET @RANDID = CONVERT ( INT, SUBSTRING( CONVERT( CHAR(20) , RAND()) , 3, 6 ) )
END
PRINT @RANDID
--还不知道弄,那我也帮不了你啦
DECLARE @RANDID INT,@count int
SET @RANDID = 0
SET @count = 0
WHILE LEN(@RANDID)<>6
BEGIN
--检查表中是否存在
SET @RANDID = CONVERT ( INT, SUBSTRING( CONVERT( CHAR(20) , RAND()) , 3, 6 ) )
select @count = count(*) from a where id = @RANDID
if (@count > 0) --存在这继续循环,直到@count = 0,则插入
SET @RANDID =SUBSTRING( CONVERT(CHAR(20),@RANDID),1,5)
END
PRINT @RANDID
--insert into a(id) values (@RANDID)
--PRINT @count
--print SUBSTRING(CONVERT(CHAR(20),@RANDID),1,5)
Select ABS(CHECKSUM(NEWID()))% 99999 + left(left(convert(varchar(20),REVERSE(RANd())),1)+ 1,1) * 100000