· 9 years ago · Nov 28, 2016, 11:42 AM
1create table File
2 (
3 id char(30) not null,
4 position int not null unique,
5 primary key (id)
6 );
7
8create table Cluster
9 (
10 id int not null,
11 next int not null unique,
12 primary key (id)
13 );
14
15
16create procedure Copy
17 @fileId char(30)
18 as
19 begin
20 declare @curr int
21 declare @prev int
22 declare @first int
23 set @curr = (select position from File where id = @fileId)
24 set @prev = null
25 set @first = null
26
27 while @curr <> -1
28 begin
29 while 1 = 1
30 begin
31 set transaction isolation level read uncommitted
32 begin transaction
33 begin try
34 @declare lastId int
35 set @lastId = 0
36 while exists (select * from Cluster where id = @lastId)
37 begin
38 set @lastId = @lastId + 1;
39 end
40
41 insert into Cluster
42 values (@lastId, -1)
43 if @prev <> null
44 begin
45 update Cluster
46 set next = @lastId
47 where id = @prev
48 end
49 set @prev = @listId
50 set @curr = (select next from Clister where id = @curr)
51 if @first = null
52 set @first = @lastId
53 waitfor delay '00:00:05'
54 commit
55 break
56 end try
57 begin catch
58 rollback transaction
59 continue
60 end catch
61 end
62 end
63 end
64 end
65
66 insert into File
67 values (@fileId + "copy", @first)
68 end