日期:2014-05-18 浏览次数:20566 次
--> --> (Roy)生成測試數據
if not object_id('申请表') is null
drop table 申请表
Go
Create table 申请表([userid] int,[applyID] nvarchar(5))
Insert 申请表
select 1,N'1' union all
select 2,N'2' union all
select 3,N'1,2,3'
Go
--> --> (Roy)生成測試數據
if not object_id('理由表') is null
drop table 理由表
Go
Create table 理由表([applyID] int,[applyName] nvarchar(2))
Insert 理由表
select 1,N'吃饭' union all
select 2,N'睡觉' union all
select 3,N'做梦'
Go
Create view v_申请表
as
select userid,
[applyName]=stuff((Select ','+[applyName] from 理由表 where ','+a.[applyID]+',' like '%,'+RTRIM([applyID])+',%' for xml path('')),1,1,'')
from 申请表 as a
go
--或用 patindex/charindex
Go
Create view v_申请表2
as
select userid,
[applyName]=stuff((Select ','+[applyName] from 理由表 where patindex('%,'+RTRIM([applyID])+',%',','+a.[applyID]+',' )>0 for xml path('')),1,1,'')
from 申请表 as a
go
select * from v_申请表2
select * from v_申请表
/*
userid applyName
1 吃饭
2 睡觉
3 吃饭,睡觉,做梦
*/
------解决方案--------------------
---还有三种方法::
分解字符串包含的信息值后然后合并到另外一表的信息
(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-12-23 广东深圳)
/*问题描述
tba
ID classid name
1 1,2,3 西服
2 2,3 中山装
3 1,3 名裤
tbb
id classname
1 衣服
2 上衣
3 裤子
我得的结果是
id classname name
1 衣服,上衣,裤子 西服
2 上衣,裤子 中山装
3 衣服,裤子 名裤
*/
-----------------------------------------------------
--sql server 2000中的写法
create table tba(ID int,classid varchar(20),name varchar(10))
insert into tba values(1,'1,2,3','西服')
insert into tba values(2,'2,3' ,'中山装')
insert into tba values(3,'1,3' ,'名裤')
create table tbb(ID varchar(10), classname varchar(10))
insert into tbb values('1','衣服')
insert into tbb values('2','上衣')
insert into tbb values('3','裤子')
go
--第1种方法,创建函数来显示
create function f_hb(@id varchar(10))
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str=''
select @str=@str+','+[classname] from tbb where charindex(','+cast(id as varchar)+',',','+@id+',')>0
return stuff(@str,1,1,'')
end
go
select id,classid=dbo.f_hb(classid),name from tba
drop function f_hb
/*
id classid name
----------- ------------- ----------
1 衣服,上衣,裤子 西服
2 上衣,裤子 中山装
3 衣服,裤子 名裤
(所影响的行数为 3 行)
*/
--第2种方法.update
while(exists (select * from tba,tbb where charindex(tbb.id,tba.classid) >0))
update tba
set classid= replace(classid,tbb.id,tbb.classname)
from tbb
where charindex(tbb.id,tba.classid)>0
select * from tba
/*
ID classid name
----------- -------------------- ----------
1 衣服,上衣,裤子 西服
2 上衣,裤子 中山装
3 衣服,裤子 名裤
(所影响的行数为 3 行)
*/
drop table tba,tbb
------------------------------------
--sql server 2005中先分解tba中的classid,然后再合并classname
create table tba(ID int,classid varchar(20),name varchar(10))
insert into tba values(1,'1,2,3','西服')
insert into tba values(2,'2,3' ,'中山装')
insert into tba values(3,'1,3' ,'名裤')
create table tbb(ID varchar(10), classname varchar(10))
insert into tbb values('1','衣服')
insert into tbb values('2','上衣')
insert into tbb values('3','裤子')
go
SELECT id , classname , name FROM
(
SELECT DISTINCT id , name FROM (select tbc.id , tbc.name , tbb.classname from
(
SELECT A.id , A.name , B.classid FROM(SELECT id , name , [classid] = CONVERT(xml,'&