· 6 years ago · Apr 16, 2019, 06:56 PM
1
2if OBJECT_ID(N'tempdb..#test_data') is not null
3 if not exists(select * from #test_data)
4 drop table if exists
5 #test_data;
6
7if OBJECT_ID(N'tempdb..#test_data') is null
8 begin
9 create table #test_data
10 (
11 row_id int not null primary key clustered,
12 a_key int not null,
13 b_key int not null,
14 val bigint not null,
15 unique(a_key, row_id),
16 unique(b_key, row_id)
17 );
18
19 with cte
20 as (
21 select top 10000
22 row_number() over (order by [object_id]) as [row_id]
23 from
24 sys.all_objects
25 ),
26 cteNumbers
27 as (
28 select
29 row_number() over (order by c1.row_id) as row_id,
30 c1.row_id as a_key,
31 c2.row_id as b_key,
32 checksum(newid()) as val
33 from
34 cte c1
35 cross join cte c2
36 )
37 insert into
38 #test_data
39 select
40 row_id,
41 a_key,
42 b_key,
43 val
44 from
45 cteNumbers;
46 end;
47
48select
49 count(*),
50 min(a_key) as min_a,
51 max(a_key) as max_a,
52 min(b_key) as min_b,
53 max(b_key) as max_b,
54 min(val) as min_val,
55 max(val) as max_val
56from
57 #test_data;