日期:2014-05-18  浏览次数:20511 次

SQL 替换
SQL code
表A:
--------------------------------------------
aID         bID
----------- -----------
107         31
110         32
111         33
112         34
113         35
114         36
117         40
108         41

表B:
--------------------------------------------
TaskIDs
----------------------
34
35
32,33,40
36
41
31

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

现在表B的存储的是表A中的bID
如何将表B里的信息根据表A的对应关系全部替换成aID
替换后的结果应该如下:
表B中的TaskIDs是表A中ID的集合(一个或多个,中间以","分隔)

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

TaskIDs
----------------------
112
113
110,111,117
114
108
107


------解决方案--------------------
SQL code

IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')
BEGIN
    DROP TABLE tba
END
GO
CREATE TABLE tba
(
    aID INT,
    bID INT
)
GO
INSERT INTO tba
SELECT 107,31 UNION
SELECT 110,         32 UNION
SELECT 111,         33 UNION
SELECT 112,         34 UNION
SELECT 113,         35 UNION
SELECT 114,         36 UNION
SELECT 117,         40 UNION
SELECT 108,         41
GO
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tbb')
BEGIN
    DROP TABLE tbb
END
GO
CREATE TABLE tbb
(
    TaskIDs VARCHAR(100)
)
GO
INSERT INTO tbb
SELECT '34' UNION 
SELECT '35' UNION 
SELECT '32,33,40' UNION 
SELECT '36' UNION 
SELECT '41' UNION 
SELECT '31'
GO
WITH t AS
(select    stuff(( select ','+ RTRIM(aID) 
                from tba AS A
                where CHARINDEX(RTRIM(bID),TaskIDs) > 0 for xml path('')),1,1,'') as Name,
                TaskIDs
from tbb AS B)

UPDATE tbb SET tbb.TaskIDs = Name FROM t WHERE tbb.TaskIDs = t.TaskIDs

SELECT * FROM tbb

------解决方案--------------------
SQL code

--分解字符串包含的信息值后然后合并到另外一表的信息
--(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  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