源代码如下:
declare @word varchar(50)
declare @word2 varchar(50)
declare thesaurus_cursor cursor for
select distinct word from thesaurus 
open thesaurus_cursor
fetch next from thesaurus_cursor into @word
while @@fetch_status=0
begin
declare words_cursor cursor local for
select word from thesaurus where code in (select code from thesaurus where word = @word) and word <> @word 
open words_cursor
fetch next from words_cursor into @word2
while @@fetch_status = 0
begin
insert into thesaurus1(word1,word2) values(@word,@word2)
fetch next from words_cursor into @word2
end
close words_cursor
deallocate words_cursor
fetch next from thesaurus_cursor into @word
end
close thesaurus_cursor
本身游标定义我觉得没错,只是当游标执行到fetch next from words_cursor into @word2的时候,便执行不下去了,是不是跟我游标定义中的in查询有关?那么该如何改造程序呢?

解决方案 »

  1.   

    declare @word varchar(50) 
    declare @word2 varchar(50) 
    declare thesaurus_cursor cursor for select distinct word from thesaurus 
    open thesaurus_cursor 
    fetch thesaurus_cursor into @word 
    while @@fetch_status=0 
    begin 
    declare words_cursor cursor local for 
    select word from thesaurus where code in (select code from thesaurus where word = @word) and word <> @word 
    open words_cursor 
    fetch next from words_cursor into @word2 
    while @@fetch_status = 0 
    begin 
    insert into thesaurus1(word1,word2) values(@word,@word2) 
    fetch next from words_cursor into @word2 
    end 
    close words_cursor 
    deallocate words_cursor 
    fetch next from thesaurus_cursor into @word 
    end 
    close thesaurus_cursor