请各位指教,如何随机不重复插入100条记录数据

QZY2003 2008-07-24 05:57:14
我的问题是要随机插入100条数据,且没有重复的4位数字,区间1000---9999之间

请各位指教!!!谢谢!!
...全文
177 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2008-07-25
  • 打赏
  • 举报
回复
declare @tb table (ID int)
declare @i int ,@a int
set @i=100
while @i>0
begin
select @a=left(rand()*1000000,4)
if not exists (select 1 from @tb where id=@a)
begin
insert into @tb select @a where not exists(select 1 from @tb where id=@a)
select @i=@i-1
end
end
select * from @tb
wzy_love_sly 2008-07-25
  • 打赏
  • 举报
回复
说if后吗?
用循环慢,偶像利用distinct的不错
wzy_love_sly 2008-07-25
  • 打赏
  • 举报
回复
你漏了
BEGIN
END了
要不你可能插入的没有100个数吧

哪里缺啊?...
lgxyz 2008-07-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wzy_love_sly 的回复:]
SQL codeif object_id('tb') is not null
drop table tb
go
create table tb(num int)

declare @i int,@num int
set @i=100
while @i>0
begin
set @num = abs(checksum(newid()))%9000+1000
if not exists(select 1 from tb where num=@num)
insert into tb select @num
set @i=@i-1
end

select * from tb order by num



1045
1086
1089
1091
1327
1460
1646
1679
1680
1703
1723 …
[/Quote]

你漏了
BEGIN
END了
要不你可能插入的没有100个数吧
Liyingyue_FFS 2008-07-25
  • 打赏
  • 举报
回复
用rand()生成一个随机数,然后随你怎么处理,while循环插入
comszsoft 2008-07-25
  • 打赏
  • 举报
回复
while 循环插入
ChinaJiaBing 2008-07-25
  • 打赏
  • 举报
回复
主键......
lgxyz 2008-07-24
  • 打赏
  • 举报
回复
DECLARE @TB TABLE (ID INT)
DECLARE @I INT,@A INT
SET @I=100
WHILE @I>0
BEGIN
SELECT @A=LEFT(RAND()*1000000,4)
IF NOT EXISTS( SELECT 1 FROM @TB WHERE ID=@A)
BEGIN
INSERT INTO @TB SELECT @A
SET @I=@I-1
END
END
fcuandy 2008-07-24
  • 打赏
  • 举报
回复
if object_id('tb') is not null
drop table tb
go

create table tb(num int)
go

insert tb
select distinct top 1000 abs(checksum(newid()))%9000+1000 from sysobjects,syscolumns
lgxyz 2008-07-24
  • 打赏
  • 举报
回复

DECLARE @TB TABLE (ID INT)
DECLARE @I INT,@A INT
SET @I=100
WHILE @I>0
BEGIN
SELECT @A=LEFT(RAND()*1000000,4)
IF NOT EXISTS( SELECT 1 FROM @TB WHERE ID=@A)
BEGIN
INSERT INTO @TB SELECT @A WHERE NOT EXISTS (SELECT 1 FROM @TB WHERE ID=@A)
SET @I=@I-1
END
END


SELECT * FROM @TB

/*

ID
-----------
2342
1679
1620
8661
9038
8816
7149
8050
2430
2629
9865
6065
1064
1271
5012
1892
2269
3576
2150
2955
4762
8650
8312
2524
2122
7707
6820
9688
5062
7395
6552
9643
7222
8078
1399
8667
9612
5030
1210
6632
9067
8376
1434
4572
7947
8340
1289
9569
5437
3506
7776
5373
2673
8298
9159
2603
4741
8523
7873
5739
1815
1816
4071
7473
8911
4800
7949
9291
2749
7113
2825
1503
9182
9280
6305
7503
6870
2557
9520
3158
5853
1074
4244
3310
8773
5877
6896
1648
5186
9650
4479
8522
1227
1865
8375
5882
7226
9135
4236
8171

(所影响的行数为 100 行)

*/
wzy_love_sly 2008-07-24
  • 打赏
  • 举报
回复
if object_id('tb') is not null
drop table tb
go
create table tb(num int)

declare @i int,@num int
set @i=100
while @i>0
begin
set @num = abs(checksum(newid()))%9000+1000
if not exists(select 1 from tb where num=@num)
insert into tb select @num
set @i=@i-1
end

select * from tb order by num


1045
1086
1089
1091
1327
1460
1646
1679
1680
1703
1723
1849
1967
2051
2170
2260
2394
2399
2480
2560
2623
3115
3133
3183
3266
3374
3405
3445
3563
3590
3657
3733
3877
3970
4081
4112
4137
4269
4457
4477
4536
4542
4553
4733
4755
4815
4842
4855
5004
5028
5105
5224
5439
5501
5914
5940
6116
6158
6160
6192
6213
6286
6392
6460
6538
6550
6702
6728
6753
6913
6947
6999
7088
7287
7370
7377
7595
7675
7918
7981
8024
8198
8212
8371
8449
8495
8536
8705
8823
8831
8917
9018
9095
9208
9256
9509
9628
9722
9811
9959
wzy_love_sly 2008-07-24
  • 打赏
  • 举报
回复
随机数不行,用identity列

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧