存储过程查找表中的内容去匹配另一张表中对应的数据,将该表查找到的数据插入到另一张表中

时间:2021-07-03 00:50:07

USE [数据库]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[表名]
as

begin
declare @Keyword varchar(max),@strs varchar(max)
--申明游标
declare MTF_Cursor CURSOR FOR select distinct Keyword from Keywords where Flag=4
--打开游标

open MTF_Cursor
fetch next from MTF_Cursor into @Keyword --将游标向下移1行,获取的数据放入之前定义的变量@@Keyword中

while @@FETCH_STATUS = 0 --返回 FETCH 执行语句时游标的状态(1:fetch获取数据成功 -1:fetch语句失败或此行不在结果集中 -2:被提取的行不存在)
begin
select ProductID,ProductName,ROW_NUMBER() over(order by ProductId) as rowss into #tempKeyword from 表名 where ProductName like '%'+@Keyword+'%'
select * from #tempKeyword

declare @numint
declare @nums int
select @nums=COUNT(1) from #tempKeyword
set @num=1

while @num<= @nums

begin
declare @productId int
declare @productName varchar(max)
declare @Content varchar(max)
select @productId= ProductId,@productName=ProductName from #tempKeyword where rowss=@num
select @Content=dbo.regexReplace(Content,'<.*?>','',1,0) from ProContent where ProductId=@productId


set @strs=RIGHT(Substring(@Content,0,CHARINDEX(@Keyword,@Content)+1),77)+Substring(@Content,CHARINDEX(@Keyword,@Content)+1,77)

insert into KeywordsContent(Keyword,[type],RecordId,Title,Summary)
values(@Keyword,'product',@productId,@productName,@strs)

set @num=@num+1

end
drop table #tempKeyword
fetch next from MTF_Cursor into @Keyword --将游标向下移1行
end
close MTF_Cursor--关闭游标
deallocate MTF_Cursor--释放游标

end

--exec KeywordContent