着力提醒:在选用iwms系统的进程中,大家会平常遇上数据内容的交替操作

/*
author:amjn
date:2003-03-28
version:1.0
function:替换text,ntext列中的字符串(不扶助粤语卡塔尔(قطر‎
*/
declare @ptr varbinary(16)
declare @amjnId int
declare @Position int,@len int
set @len=datalength(‘ILOVEHJL’)  
declare wux_Cursor scroll Cursor
for
select textptr([Name]),[amjnId] from USA201
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@amjnId
while @@fetch_status=0
begin
  select @Position=patindex(‘%ILOVEHJL%’,[Name]) from USA201 where
[amjnId]=@amjnId
  while @Position>0
  begin
    set @Position=@Position-1
    updatetext USA201.[Name] @ptr @Position @len ‘i love hjl’
    select @Position=patindex(‘%ILOVEHJL%’,[Name]) from USA201
where [amjnId]=@amjnId
  end
  fetch next from wux_Cursor into @ptr,@amjnId
end
close wux_cursor
deallocate wux_cursor
go

一、问题陈说: 1。在Sql Server 中,ntext/text/image
字段不容许采取replace函数替换内容;
2。通过convert字段转变,可以把ntext字段转变为varchar,然后用Relpace函数替换,不过,此办法,对于字段长度超过8000的ntext字段无效。
二、难点一挥而就 收拾通用存储进程,代码如下: 复制代码 代码如下: CREATE procedure
[dbo].[Proc_UpdateNTextField] @TargetTable nvarchar(1000State of Qatar,
–目的表名 @TargetField nvarchar(1000卡塔尔, –目的字段名 @PKFieldnvarchar(1000State of Qatar, –该表主键字段名 @otxt nvarchar(1000State of Qatar,
–供给替换的字符串 @ntxt nvarchar(1000卡塔尔(قطر‎ –替换后的字符串 as begin
declare @SqlStr nvarchar(4000卡塔尔(قطر‎ set @SqlStr = ‘ declare @txtlen int ‘ set
@SqlStr = @SqlStr + ‘ set @txtlen = len(”’ + @otxt + ”’State of Qatar ‘ set @SqlStr
= @SqlStr + ‘ declare @pos int ‘ set @SqlStr = @SqlStr + ‘ set @pos = 0
‘ set @SqlStr = @SqlStr + ‘declare curs cursor local fast_forward for
select ‘ set @SqlStr = @SqlStr + @PKField + ‘ , textptr(‘ + @TargetField
+’) from ‘ + @TargetTable +’ where ‘ + @TargetField + ‘ like ”%’ +
@otxt +’%”’ set @SqlStr = @SqlStr + ‘ declare @ptr binary(16) ‘ set
@SqlStr = @SqlStr + ‘ declare @id char(32) ‘ set @SqlStr = @SqlStr + ‘
open curs ‘ set @SqlStr = @SqlStr + ‘ fetch next from curs into @id,
@ptr ‘ set @SqlStr = @SqlStr + ‘ while @@fetch_status = 0 ‘ set @SqlStr
= @SqlStr + ‘ begin ‘ set @SqlStr = @SqlStr + ‘ select @pos=
patindex(”%’ + @otxt + ‘%”,ProductDesc) from ProductTemp where
ProductID=@id ‘ set @SqlStr = @SqlStr + ‘ while @pos0 ‘ set @SqlStr =
@SqlStr + ‘ begin ‘ set @SqlStr = @SqlStr + ‘ set @pos=@pos-1 ‘ set
@SqlStr = @SqlStr + ‘ updatetext ‘ + @TargetTable + ‘.’ +@TargetField +
‘ @ptr @pos @txtlen ”’ + @ntxt + ”’ ‘ set @SqlStr = @SqlStr + ‘ select
@pos= patindex(”%’ + @otxt + ‘%”,ProductDesc) from ProductTemp where
ProductID=@id ‘ set @SqlStr = @SqlStr + ‘ end ‘ set @SqlStr = @SqlStr +
‘ fetch next from curs into @id, @ptr ‘ set @SqlStr = @SqlStr + ‘ end ‘
set @SqlStr = @SqlStr + ‘ close curs ‘ set @SqlStr = @SqlStr + ‘
deallocate curs ‘ EXECUTE sp_executesql @SqlStr end

在应用iwms系统的进度中,我们会不常碰着数据内容的轮换操作。在报告我们怎么样替换数据内容前面,笔者建议我们先掌握一下SQL
Server数据库的数额存款和储蓄类型:

SQL Server数据类型:

如上是数据库的幼功知识,是做网址的相恋的人都应有精晓的内容,所以建议我们都言近旨远看一下。

数码替换平日都发出在字符串数据字段中,除了ntext类型字段以外的别样字符串数据字段都能够选择以下的sql语句进行替换:

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注