光标检查单词

时间:2014-06-13 13:12:18

标签: sql sql-server

我在sql server表中有数百万字的Engtitle字段。

我想检查表中每个单词重复的次数,条件如下:

select distinct engtitle from table
where a1 = 'EHD'

我将通过上述查询获得200万条记录。我想在同一个表格中检查上述查询中的每个单词。具有以下条件:

假设@variable是我从上面的查询得到的一个单词

select COUNT(*) from table
where engtitle like '%@variable %' 

我希望在表格中插入每个单词及其计数。表2(engtitle,count)

对于上述情况,我尝试了以下光标:

declare @Engword varchar(max)

DECLARE word_cursor CURSOR FOR
select distinct engtitle from table
where a1 = 'EHD'
ORDER BY Engtitle;

OPEN Word_cursor;

FETCH NEXT FROM Word_cursor
INTO @Engword;
Print @Engword + 'f'

WHILE (select COUNT(*) from table where engtitle like '%@Engword %') > 0  and @@FETCH_STATUS = 0
BEGIN
Print @Engword + 's'

   Insert into Table1(Engtitle,[count])
   Values(@Engword,(select COUNT(*) from table 
where engtitle like '%@Engword %'))

IF (select COUNT(*) from table 
where engtitle like '%@Engword %' ) < 0
Begin
Insert into Table1(Engtitle,[count])
   Values(@Engword,(select COUNT(*) from table 
where engtitle like '%@Engword %'))
END


   FETCH NEXT FROM Word_cursor
   INTO @Engword;
END

CLOSE Word_cursor;
DEALLOCATE Word_cursor;
GO

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你只想将每个单词的出现次数插入另一个表中。

我认为如果你只是选择一个小组,这应该会很有效:

insert into table1 (
select distinct engtitle, count(0) from table
where a1 = 'EHD'
and engtitle like '@variable'
group by engtitle having count(0) > 0);