/*--ntext搜索
按 tb 表中的 keyword 在 ta 中查找 content
列出每个 keyword 在 content 中的具体位置
--邹建 2004.07(引用请保留此信息)--*/
--测试数据
create table ta(id int identity(1,1),content ntext)
insert ta select '我是中国人我是中国人'
union all select '中国人民爱中国 中国人民爱中国 中国人民爱中国 中国人民爱中国'
create table tb(keyword nvarchar(100))
insert tb select '中'
union all select '中国'
go
/*=================处理========================*/
if exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序数表]
GO
--为了效率,所以要一个辅助表配合
select top 4000 id=identity(int,1,1) into 序数表
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
go
--创建处理的存储过程
create proc p_search
as
create table #t(id int,keyword nvarchar(100),position int)
declare @s Nvarchar(4000),@keyword nvarchar(100)
declare @id int,@i int,@ilen int
declare tb cursor local for
select a.id,b.keyword,position=charindex(b.keyword,a.content)-1,ilen=4000-len(b.keyword)
from ta a,tb b
where charindex(b.keyword,a.content)>0
open tb
fetch tb into @id,@keyword,@i,@ilen
while @@fetch_status=0
begin
select @s=substring(content,@i+1,4000)
from ta where id=@id
while @s<>''
begin
insert #t(id,keyword,position)
select @id,@keyword,id+@i
from 序数表
where charindex(@keyword,@s,id)=id
select @i=@i+@ilen,@s=substring(content,@i+1,4000)
from ta where id=@id
end
fetch tb into @id,@keyword,@i,@ilen
end
close tb
deallocate tb
select * from #t
go
--调用示例
exec p_search
go
--删除测试
drop table 序数表,ta,tb
drop proc p_search
/*--测试结果
id keyword position
----------- --------- ----------
1 中 3
1 中 8
1 中国 3
1 中国 8
2 中 1
2 中 6
2 中 9
2 中 14
2 中 17
2 中 22
2 中 25
2 中 30
2 中国 1
2 中国 6
2 中国 9
2 中国 14
2 中国 17
2 中国 22
2 中国 25
2 中国 30
(所影响的行数为 20 行)
--*/
分享到:
相关推荐
在不确定text或ntext字段长度的时候 SQL Server 中 text或ntext 字段内容替换方法
今天将一个ACC的数据库转换成ms-sql以后发现在使用replace替换语句的时候出现: SQL中函数 replace 的参数 1 的数据类型 ntext 无效。找了半天找到了解决办法: 因为ntext需要转化下 用如下语句即可解决: 代码如下:...
Toolbelt_Blazor_I18nText-master.rar
不过,在我使用过程中,发现,如果表中包含了ntext字段,就会出错,提示 不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版本将 ntext 数据或仅使用Unicode排序规则的 Unicode 数据发送到客户端。 查了一下,发现...
代码如下: —ntext数据类型字符替换 create table tt ( sid INT IDENTITY(1,1), cont ntext ) go insert into tt(cont) values(N’fd sad fdsa 涂聚文工团 缔友计算机信息技术有限公司 可能性 桔柑 ‘) go update tt...
通过convert字段转换,可以把ntext字段转换为varchar(8000),然后用Relpace函数替换,不过,此方法,对于字段长度大于8000的ntext字段无效。 二、问题解决 整理通用存储过程,代码如下: 代码如下: CREATE ...
数据库注入修复 批量替换 一个数据库 中所有表中所有记录 包括ntext,text
SQLserver查询数据类型为ntext是空或NULL值的方法
SQL中NTEXT字段内容显示<long text>的原因
本篇文章是对char、varchar、text和nchar、nvarchar、ntext的区别进行了详细的分析介绍,需要的朋友参考下
如果是表里面没有ntext字段,可以用以下代码: 复制代码 代码如下:<?php // Connect to MSSQL $link = mssql_connect(‘KALLESPCSQLEXPRESS’, ‘sa’, ‘dsc’); if(!$link || !mssql_select_db(
Blazor国际化(I18n)文本 概要 该软件包是在Blazor Web App中本地化文本的另一种方法!... 您可以将使用“ Blazor I18nText”本地化的库打包为NuGet包。 注意 现在,Blazor WebAssembly从v.3.2 Pr
sql中varchar和nvarchar的区别与使用方法Sqlserver中char,nchar,varchar与Nvarchar的区别分析基于SQL Server中char,nchar,varchar,nvarchar的使用区别深入char、varchar、text和nchar、nvarchar、ntext的区别详解SQL...
过程需要参数