您的位置:首页 > 其它

字段里字符串的处理问题

2007-06-29 16:29 681 查看
原帖地址:

http://community.csdn.net/Expert/topic/3244/3244874.xml?temp=.5755274
表一:MenuTree?
Id??????????????????? context?????????????????
-----------? ---------------------------
1????????????????????? 商场监测系统?
3????????????????????? 综合报表分析?
4????????????????????? 销售月报表?
6????????????????????? ◇销售数据采集?
7????????????????????? ◇监测商场管理?
9????????????????????? ◇监测产品管理?
?
表二:? jslmb?
?
jsbh????? jsmc?????????????????? kgllm????????
------- ------------? -------------------------
1??????? 查询所有数据????????? '1','2','3','4','5'
2??????? 查询西北区数据??????? '1','2','4','5','9'
?
?

如何把删除表二的Kgllm(可管理栏目)字段里的表一不存在的栏目编号剔除
?
例如:
表一的IdZ字段:"2"和"5"值已不存在了。

我要把表二的kgllm字段里的"2"和"5"值剔除掉,其它值如"1"、"3"等值要保留

--------------------------------------------------------------------------------------

--测试数据
create table MenuTree(id int,context varchar(20))
insert MenuTree select 1,'商场监测系统'
union? all????? select 3,'综合报表分析'
union? all????? select 4,'销售月报表'
union? all????? select 6,'◇销售数据采集'
union? all????? select 7,'◇监测商场管理'
union? all????? select 9,'◇监测产品管理'

create table jslmb(jsbh int,jsmc varchar(20),kgllm varchar(8000))
insert jslmb select 1,'查询所有数据'? ,'''1'',''2'',''3'',''4'',''5'''
union? all?? select 2,'查询西北区数据','''1'',''2'',''4'',''5'',''9'''
go

--创建一个辅助处理数据的表(这里的8000根据你的 kgllm 的大小确定,如果你的 kgllm 长度是1000,就改为1000)
select top 8000 id=identity(int,1,1) into 序数表
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
go

--更新处理
select a.jsbh,kgllm=substring(a.kgllm,b.id,charindex(',',a.kgllm+',',b.id)-b.id)
?,re=cast('' as varchar(8000))
into #t
from jslmb a,序数表 b,MenuTree c
where b.id<=len(a.kgllm)
?and substring(','+a.kgllm,b.id,1)=','
?and stuff(substring(a.kgllm,b.id,charindex(',',a.kgllm+',',b.id)-b.id-1),1,1,'')
??=c.id
order by a.jsbh

declare @jsbh int,@re varchar(8000)
update #t set @re=case @jsbh when jsbh then @re+','+kgllm else kgllm end
?,re=@re,@jsbh=jsbh

update a set kgllm=b.re
from jslmb a,(
?select jsbh,re=max(re) from #t group by jsbh
)b where a.jsbh=b.jsbh

drop table #t

--显示更新结果
select * from jslmb
go

--删除测试数据
drop table MenuTree,jslmb
drop table 序数表

/*--测试结果

jsbh??????? jsmc???????????????? kgllm??????????
----------- -------------------- ----------------
1?????????? 查询所有数据?????????? '1','3','4'
2?????????? 查询西北区数据???????? '1','4','9'

(所影响的行数为 2 行)
--*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: