数据将如下所示:
id | data ----|--------- 1 | AABBCC 2 | FFDD 3 | TTHHJJKKLL
我想要的结果是
id | data ----|--------- 1 | AA 1 | BB 1 | CC 2 | FF 2 | DD 3 | TT 3 | HH 3 | JJ 3 | KK 3 | LL
我有一个使用游标的SQL代码
DECLARE @table2 table ( id INTEGER, data VARCHAR(500)) DECLARE Cur CURSOR FOR SELECT id FROM table1 OPEN Cur WHILE ( @@FETCH_STATUS = 0 ) BEGIN DECLARE @LoopNum INTEGER DECLARE @tempID INTEGER DECLARE @tempDATA VARCHAR(255) FETCH NEXT FROM Cur INTO @tempID SET @tempDATA = SELECT data from table1 where id = @teampID SET @LoopNUM = 0 WHILE @LoopNum< len(@tempDATA) / 2 BEGIN INSERT INTO table2 (id, data) VALUES( @tempID, SUBSTRING(@tempDATA, @LoopNum * 2 +1, 2)) SET @LoopNum = @LoopNum + 1 END END CLOSE Cur DEALLOCATE Cur SELECT * FROM table2
我不想使用CURSOR,因为它太慢了,我有很多数据。有谁对此有个好主意吗?是否有可能?