欢迎大家在这里学习SQL Server存储过程!下面是我们给大家整理出来的精彩内容。希望大家在这里学习!
declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('老字符串')
declare wux_Cursor scroll Cursor
for
select textptr([字段名]),[key字段名] from [数据表名]
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%老字符串%',[字段名]) from [数据表名] where [key字段名]=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext [数据表名].[字段名] @ptr @Position @len '新字符串'
select @Position=patindex('%老字符串%',[字段名]) from [数据表名] where [key字段名]=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go
比如,替换iwms文章数据表(iwms_news)中的标题字段(content,ntext类型字段)的部分内容,我们应该这么写
declare @ptr varbinary(16)
declare @artId int
declare @Position int,@len int
set @len = datalength('老字符串')
declare wux_Cursor scroll Cursor
for
select textptr([content]),[articleid] from iwms_news
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@artId
while @@fetch_status=0
begin
select @Position=patindex('%老字符串%',[content]) from iwms_news where [articleid]=@artId
while @Position>0
begin
set @Position=@Position-1
updatetext iwms_news.[content] @ptr @Position @len '新字符串'
select @Position=patindex('%老字符串%',[content]) from iwms_news where [articleid]=@artId
end
fetch next from wux_Cursor into @ptr,@artId
end
close wux_cursor
deallocate wux_cursor
go
ok了,需要注意的是:存储过程只能在SQL Server查询分析器中执行。
另外,由于iwms数据库结构的问题,有分页的文章内容需要先后对iwms_news和iwms_pages两个表内容进行替换操作。
好了,SQL Server存储过程内容就给大家介绍到这里了。希望大家继续关注我们的网站!
相关推荐:
sqlserver使用窗口函数实现分页的方法