7 回答

TA貢獻1921條經(jīng)驗 獲得超9個贊
1 select * from Table_1; 2 create table #table_temp( 3 id int identity(1,1) not null, 4 txt varchar(50)); 5 declare mycur cursor for select txt from Table_1 6 open mycur 7 declare @t_txt varchar(50),@t_txt1 varchar(50) 8 fetch next from mycur into @t_txt 9 while(@@FETCH_STATUS = 0) 10 begin 11 if CHARINDEX(',',@t_txt)> 0 12 begin 13 while(CHARINDEX(',',@t_txt)> 0 ) 14 begin 15 set @t_txt1 = substring(@t_txt,1,CHARINDEX(',',@t_txt)-1) 16 insert #table_temp(txt) values(@t_txt1) 17 set @t_txt = SUBSTRING(@t_txt,CHARINDEX(',',@t_txt)+1,LEN(@t_txt) - LEN(@t_txt1)) 18 end 19 insert into #table_temp(txt) values (@t_txt) 20 end 21 else 22 begin 23 insert #table_temp(txt) values(@t_txt) 24 end 25 fetch next from mycur into @t_txt 26 end 27 close mycur 28 deallocate mycur 29 select * from #table_temp 30 drop table #table_temp
?
@yj_smile:?create table #table_temp(
id int identity(1,1) not null,
txt varchar(50));
declare mycur cursor for select txt from Table_1
open mycur
declare @t_txt varchar(50),@t_txt1 varchar(50)
fetch next from mycur into @t_txt
while(@@FETCH_STATUS = 0)
begin
if CHARINDEX(',',@t_txt)> 0
begin
? while(CHARINDEX(',',@t_txt)> 0 )
? begin
? set @t_txt1 = substring(@t_txt,1,CHARINDEX(',',@t_txt)-1)
? insert #table_temp(txt) values(@t_txt1)
? set @t_txt = SUBSTRING(@t_txt,CHARINDEX(',',@t_txt)+1,LEN(@t_txt) - LEN(@t_txt1))
? end
? insert into #table_temp(txt) values (@t_txt)
end
else
begin
insert #table_temp(txt) values(@t_txt)
end
fetch next from mycur into @t_txt
end
close mycur
deallocate mycur
select * from #table_temp
drop table? #table_temp
不知道怎么提交代碼?。。?!
以上是用游標實現(xiàn)的

TA貢獻1859條經(jīng)驗 獲得超6個贊
create table #table_temp( id int identity(1,1) not null, txt varchar(50)); declare mycur cursor for select txt from Table_1 open mycur declare @t_txt varchar(50),@t_txt1 varchar(50) fetch next from mycur into @t_txt while(@@FETCH_STATUS = 0) begin if CHARINDEX(',',@t_txt)> 0 begin while(CHARINDEX(',',@t_txt)> 0 ) begin set @t_txt1 = substring(@t_txt,1,CHARINDEX(',',@t_txt)-1) insert #table_temp(txt) values(@t_txt1) set @t_txt = SUBSTRING(@t_txt,CHARINDEX(',',@t_txt)+1,LEN(@t_txt) - LEN(@t_txt1)) end insert into #table_temp(txt) values (@t_txt) end else begin insert #table_temp(txt) values(@t_txt) end fetch next from mycur into @t_txt end close mycur deallocate mycur select * from #table_temp drop table #table_temp
- 7 回答
- 0 關注
- 1318 瀏覽
添加回答
舉報