ntext搜索关键字

/*--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 行)
--*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是在所有表的数据中搜索关键字,返回表名及字段名的SQL语句: ``` SELECT DISTINCT c.TABLE_NAME, c.COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS c INNER JOIN INFORMATION_SCHEMA.TABLES t ON c.TABLE_NAME = t.TABLE_NAME WHERE c.DATA_TYPE IN ('char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext', 'date', 'datetime', 'datetime2', 'time', 'smalldatetime') AND t.TABLE_TYPE = 'BASE TABLE' AND EXISTS ( SELECT * FROM sys.columns sc INNER JOIN sys.objects so ON sc.object_id = so.object_id WHERE sc.name = c.COLUMN_NAME AND so.name = c.TABLE_NAME AND so.type = 'U' AND ( sc.system_type_id IN (167, 175, 231, 239) -- char, varchar, text, nchar, nvarchar, ntext OR sc.system_type_id IN (40, 41, 42, 43, 58, 61, 62, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 165, 167, 173, 175, 189, 231, 239) -- date, datetime, datetime2, time, smalldatetime ) ) AND EXISTS ( SELECT * FROM sys.objects so INNER JOIN sys.columns sc ON so.object_id = sc.object_id INNER JOIN sys.types st ON sc.system_type_id = st.system_type_id AND sc.user_type_id = st.user_type_id INNER JOIN sys.schemas ss ON so.schema_id = ss.schema_id WHERE so.type = 'U' AND so.name = c.TABLE_NAME AND sc.name = c.COLUMN_NAME AND ( OBJECT_DEFINITION(so.object_id) LIKE '%'+@keyword+'%' OR ss.name+'.'+so.name LIKE '%'+@keyword+'%' OR st.name LIKE '%'+@keyword+'%' ) ) ORDER BY c.TABLE_NAME, c.COLUMN_NAME; ``` 需要将其中的 @keyword 替换为需要搜索关键字
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值