復制代碼 代碼如下:
declare @t varchar(255),@c varchar(255)
declare table_cursor cursor for select a.name,b.name
from sysobjects a,syscolumns b ,systypes c
where a.id=b.id and a.xtype='u' and c.name
in ('char', 'nchar', 'nvarchar', 'varchar','text','ntext'/* --這裡如果你的text(ntext)類型沒有超過8000(4000)長度,才可以使用*/)
declare @str varchar(500),@str2 varchar(500)
set @str='a' /*這裡是你要替換的字符*/
set @str2='' /*替換後的字符*/
open table_cursor
fetch next from table_cursor
into @t,@c while(@@fetch_status=0)
begin exec('update [' + @t + '] set [' + @c + ']=replace(cast([' + @c + '] as varchar(8000)),'''+@str+''','''+ @str2 +''')')
fetch next from table_cursor
into @t,@c end close table_cursor deallocate table_cursor;
小於8000的處理
update buyok_Orderlist set Notes=replace(cast(Notes as varchar(8000)),'a','')
如果text/ntext超過8000/4000,看如下例子<沒有試過>
declare @pos int
declare @len int
declare @str nvarchar(4000)
declare @des nvarchar(4000)
declare @count int
set @des ='<requested_amount+1>'--要替換成的值
set @len=len(@des)
set @str= '<requested_amount>'--要替換的字符
set @count=0--統計次數.
WHILE 1=1
BEGIN
select @pos=patINDEX('%'+@des+'%',propxmldata) - 1
from 表名
where 條件
IF @pos>=0
begin
DECLARE @ptrval binary(16)
SELECT @ptrval = TEXTPTR(字段名)
from 表名
where 條件
UPDATETEXT 表名.字段名 @ptrval @pos @len @str
set @count=@count+1
end
ELSE
break;
END
select @count