日期:2014-05-17  浏览次数:20850 次

为什么replace函数替换不了 ,请高手指点
想把一个字符串中所有出现在delete_author表中的字符都删除掉(也就是替换成空)
delete_author表中的内容是:

我写成一个存储过程:(有些输出是为了测试)
create proc proc_del_special 
@input_str varchar(100),@output_str varchar(100) output
as
declare @author  varchar(256),@delsym varchar(10),@temp_str varchar(100)
declare del_cur scroll cursor for select del_author from delete_author
open del_cur
fetch next from del_cur into @delsym
set @author=@input_str
while @@fetch_status=0
     begin 
         print replace(@author,@delsym,'')
        set @temp_str=replace(@author,@delsym,'')
         set @author=@temp_str
         print @author
         print @delsym
         fetch next from del_cur into @delsym
     end 
set @output_str=@author
close del_cur
deallocate del_cur


测试例子:
declare @a1 varchar(100),@a2 varchar(100)
set @a1='(美)啊那编译'
exec proc_del_special @a1,@a2 output
select @a2
 
执行结果还是:(美)啊那编译 
执行过程中的消息:
美)啊那编译
(美)啊那编译
主编      
(美)啊那编译
(美)啊那编译
编译      
(美)啊那编译
(美)啊那编译
(美)      
(美)啊那编译
(美)啊那编译
(英)      
(美)啊那编译
(美)啊那编译
(苏)      
(美)啊那编译
(美)啊那编译
[等]      
(美)啊那编译
(美)啊那编译
(日)      
(美)啊那编译
(美)啊那编译
编著      
(美)啊那编译
(美)啊那编译
(西)      

(1 行受影响)

就是replace函数没有替换成功,这是为什么啊 ,请哪位大侠指点一下 ,在线等,非常感谢了


------解决方案--------------------
尽量少用游标,用函数也可以实现:

--drop table tb

create table tb(v nvarchar(100))

insert into tb
select '(英)啊那编译' union all
select '小泉纯一郎(日)' union all
select '皇家马德里对(西)主编' union all
select '奥巴马(美)编著' union all
select '中国[等]'


create table delete_author (del_author nvarchar(50))

insert into delete_author
select '主编' union all
select '编译' union all
select '(美)' union all
select '(英)' union all
select '(苏)' union all
select '[等]' union all
select '(日)' union all
select '编著' union all
select '(西)' 



if exis