Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1183507
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: Mysql/postgreSQL

2009-03-20 00:53:37

declare @table nvarchar(40); -- 查询表名
declare @textfield nvarchar(40); -- 查询列名(text类型)
declare @findstr nvarchar(400); -- 查找的字符串(非空)
declare @step int; -- 每次从text字段截取的字符串长度
declare @findcount int; -- 出现次数
declare @sql nvarchar(400);
declare @str nvarchar(4000);
declare @findpos int;
declare @pos int;
declare @innerpos int;

set @table = 'content';
set @textfield = 'aboutus';
set @findstr = '';
set @step = 1000;
set @pos = 1;
set @findpos = 0;
set @findcount = 0;

while(@pos = 1 or len(@str) = @step)
begin
    set @sql = N'select @str = substring(cast('+@textfield+' as ntext), '+cast(@pos as nvarchar)+', '+cast(@step as nvarchar)+') from '+@table;
    --select @sql;
    exec sp_executesql @sql, N'@str nvarchar(4000) output', @str output;
    --select @str;
    set @innerpos = 1;
    while(@innerpos = 1 or @findpos > 0)
    begin
        select @findpos = charindex(@findstr, @str, @innerpos);
        if @findpos > 0
            set @findcount = @findcount + 1;
        set @innerpos = @findpos + len(@findstr);
    end
    set @pos = @pos + len(@str) - (len(@findstr) - 1);
end

select @findcount;

阅读(5465) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~