字段里字符串的处理问题
2004-08-05 20:48
639 查看
原帖地址:
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 行)
--*/
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 行)
--*/
相关文章推荐
- Tomcat5.0+mysql配置JDBCRealm,DBCP,ssl,中文乱码处理
- 操作ini文件
- ASP.NET 2.0数据缓存功能简介(原作)
- 第一桶水
- Tomcat 相关资料
- 网上看的实现职业规划的几点看法 Javcs[原作]
- VC下利用多线程实现文件的快速检索
- 2004-08-05工作日志
- 想不通的Exchange Server问题--想通了
- 又跑老婆家里了
- DataGrid也玩分页
- EJB编程及J2EE系统架构和设计
- 基金系统的初步研究(1)
- 中新网8月5日电 据京华时报报道,来自建设部下属的全国超限高层建筑工程抗震设防审查专家委员会的消息证实,北京奥运会主会场--国家体育场(“鸟巢”)没有通过该委员会今年7月组织的“抗震设防”审查。
- 一个程序详细研究DataReader
- 世界上最美的路
- [今日]梦,雨,鞋
- UML Rose的使用(四) --属性与操作
- VGCrypt PE Encryptor v0.75 Beta Source
- 一种很Cool的语言