以前写过一个,原表名为t_test,有三列:c1,c2,c3,分别与你的列1,列3,列2对应,用来处理数据的存储过程如下:
说明:t_test_tmp是用来存放处理后数据的表
CREATE PROCEDURE p_testCur
as
declare @c1 as varchar(50)
declare @c2 as varchar(50)
declare @c3 as varchar(50)
declare @c as varchar(500)
set @c=''
delete from t_test_tmp
DECLARE cur_test1 CURSOR FOR
SELECT distinct c1,c2
FROM t_test
order by c1,c2
OPEN cur_test1
FETCH NEXT FROM cur_test1 into @c1,@c2
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE cur_test2 CURSOR FOR
SELECT c3
FROM t_test
where c1=@c1 and c2=@c2
order by c3
set @c=''
OPEN cur_test2
FETCH NEXT FROM cur_test2 into @c3
WHILE @@FETCH_STATUS = 0
BEGIN
set @c=@c+@c3+','
FETCH NEXT FROM cur_test2 into @c3
END
set @c=left(@c,len(@c)-1)
insert into t_test_tmp values(@c1,@c2,@c)
CLOSE cur_test2
DEALLOCATE cur_test2
FETCH NEXT FROM cur_test1 into @c1,@c2
END
CLOSE cur_test1
DEALLOCATE cur_test1
select * from t_test_tmp
GO