在论坛中出现的比较难的sql问题:41(循环替换循环替换关键字)

在论坛中出现的比较难的sql问题:41(循环替换循环替换关键字)

2023年6月27日发(作者:)

在论坛中出现的⽐较难的sql问题:41(循环替换循环替换关键字)最近,在论坛中,遇到了不少⽐较难的sql问题,虽然⾃⼰都能解决,但发现过⼏天后,就记不起来了,也忘记解决的⽅法了。所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。为什么replace函数替换不了 ,请⾼⼿指点:/topics/390625259想把⼀个字符串中所有出现在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 tbcreate table tb(v nvarchar(100))insert into tbselect '(英)啊那编译' union allselect '⼩泉纯⼀郎(⽇)' union allselect '皇家马德⾥对(西)主编' union allselect '奥巴马(美)编著' union allselect '中国[等]'create table delete_author (del_author nvarchar(50))insert into delete_authorselect '主编' union allselect '编译' union allselect '(美)' union allselect '(英)' union allselect '(苏)' union allselect '[等]' union allselect '(⽇)' union allselect '编著' union allselect '(西)'

if exists(select * from s where name = 'fn_replace_symbol') drop function _replace_symbol;gogocreate function _replace_symbol(@n nvarchar(1000))returns nvarchar(1000)asbegindeclare @i int;declare @count int;set @i = 1set @count = (select count(*) from delete_author);

while @i <= @countbegin ;with t as ( select del_author, row_number() over(order by @@servername) as rownum from delete_author )

select @n = replace(@n,(select del_author from t where rownum = @i),'') set @i = @i + 1endreturn @nendgo--替换效果select * , _replace_symbol(v) as 替换完后的字符from tb/*v 替换完后的字符(英)啊那编译 啊那⼩泉纯⼀郎(⽇) ⼩泉纯⼀郎皇家马德⾥对(西)主编 皇家马德⾥对奥巴马(美)编著 奥巴马中国[等] 中国*/

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687817531a47740.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信