|
create table test_1 (id int, name char (10), class varchar (50))
go
insert test_1
select 12, 'a', '34 || 35 || 36 'union all
select 13, 'b', '34 || 36 'union all
select 14, 'c', '33 || 34 'union all
select 15, 'd', '34 || 35 'union all
select 16, 'e', '34' union all
select 17, 'f', '34 || 37 '
go
create table test_2 (id int, name char (10), class varchar (50))
go
select 1
while @@ rowcount> 0
-When the begin ... end statement block is executed, the number of rows affected by the execution is 0, which means that the lessons, score1, and score2 fields of the test_1 table have been cleared, and the loop is complete
begin
insert test_2
select id, name, class
from test_1
where charindex ('|', class) = 0 and class <> ''
insert test_2
select id, name, left (class, charindex ('|', class) -1) as class-take the first class name
from test_1
where charindex ('|', class)> 0
update test_1 set class = ''
where charindex ('|', class) = 0
update test_1 set
class = substring (class, charindex ('|', class) + 2, len (class) -charindex ('|', class))-take all classes except the first class
where charindex ('|', class)> 0
end
go
select * from test_2
order by id, name, class
drop table test_1, test_2
/ *
id name class
12 a 34
12 a 35
12 a 36
13 b 34
13 b 36
14 c 33
14 c 34
15 d 34
15 d 35
16 e 34
17 f 34
17 f 37
* / |
|