网站链接: 环球农商网
当前位置: 首页 > 资讯动态  > 平台资讯

SQL Server数据库内容替换方法

2019/3/14 8:48:36 人评论

在使用iwms系统的过程中,我们会经常遇到数据内容的替换操作。在告诉大家如何替换数据内容之前,我建议大家先了解一下SQL Server数据库的数据存储类型:

SQL Server数据类型:

以上是数据库的基础知识,是做网站的朋友都应该知道的内容(无论你使用什么cms),所以建议大家都耐心看一下。

数据替换一般都发生在字符串数据字段中,除了ntext类型字段以外的其他字符串数据字段都可以使用以下的sql语句进行替换:

update [swf_Upload] set [Dir] = replace([Dir],'200901/14','200901/15')update [swf_Content] set [Description] =replace([Description],'200901/14','200901/15')update [swf_Content_01] set [content] = replace(convert(varchar(4000), [content]),'200901/14','200901/15')

UPDATE [数据表名] SET [字段名] = REPLACE([字段名],'老字符串','新字符串')
比如,替换iwms文章数据表(iwms_news)中的标题字段(title)的部分内容,我们应该这么写:

UPDATE [iwms_news] SET [title] = REPLACE([title],'老字符串','新字符串')
上面的sql语句在iwms后台的sql执行里面可以直接执行,基本上可以搞定所有的替换操作,但是由于ntext数据长度的原因,这一方法对ntext类型字段无效。那我们该用什么方法替换ntext类型字段的内容呢?方法有两种:

一是类型转换,将ntext类型转换为varchar类型,然后再用replace。适合于单页内容最大长度<4000的文章。

update [数据表名] set [字段名] = replace(convert(varchar(4000), [字段名]),'老字符串','新字符串')
比如,替换iwms文章数据表(iwms_news)中的标题字段(content,ntext类型字段)的部分内容,我们应该这么写:

update iwms_news set [content] = replace(convert(varchar(4000),[content]),'老字符串','新字符串')

二是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两个表内容进行替换操作。

 

相关资讯

  • document.cookie:客户端操作cookie

    我们已经知道,在 document 对象中有一个 cookie 属性。但是 Cookie 又是什么?&ldquo;某些 Web 站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称为 Cookie。&rdquo;&mdash;&mdash; MSIE 帮助。一般来说,Cookies 是 CGI 或类似,比 HTML …

    2019/4/12 8:18:11
  • 用ASP实现分级权限控制

    本文实现的是一个帐务管理系统中分级权限的控制,程序使用ASP和JavaScript编写,在装有IIS4.0的win NT服务器上运行,速度快,易维护。  权限级别划分如下:  ①、院长和财务科长:不能输入,可以无限制查询、统计;  ②、副院长:不能输入,可以查询、统计其分管部…

    2019/4/12 8:18:11
  • ASP实现Rewrite模拟生成静态页效

    以前刚刚懂404.asp的时候,曾经幻想把所有程序代码写到404.asp中,实现一个模拟生成静态网页的站,如果程序小还可以,用404.asp实现Rewrite还是一个不错的选择,如果程序代码多达100000行,恐怕就要开始爬了  直到看到asp的Server.Transfer,用404模拟生成静态页的站的念…

    2019/4/12 8:18:11
  • ASP随机涵数生成100条8位字母和数字混合密码

    <%for i = 1 to 100%><%Randomizepass=""Do While Len(pass)<8 随机密码位数num1=CStr(Chr((57-48)*rnd+48)) 0~9num2=CStr(Chr((122-97)*rnd+97)) a~zpass=pass&num1&num2loop原创:www.devdao.com%><%=pass%><br><%next…

    2019/4/12 8:18:11

共有条评论 网友评论

验证码: 看不清楚?