有两张表如下
表t1
F1 F2
--------------------------
a 我的
b 你的
c 他的
d 大家的
---------------------------表t2
F1 OTHER
---------------------------
a,b 027
c 961
a,c 091
---------------------------求助怎样得到如下的表
F1 列2 列3
---------------------------
a,b 我的,你的 027
c 他的 961
a,c 我的,他的 091
---------------------------
表t1
F1 F2
--------------------------
a 我的
b 你的
c 他的
d 大家的
---------------------------表t2
F1 OTHER
---------------------------
a,b 027
c 961
a,c 091
---------------------------求助怎样得到如下的表
F1 列2 列3
---------------------------
a,b 我的,你的 027
c 他的 961
a,c 我的,他的 091
---------------------------
go
--> -->
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([F1] nvarchar(1),[F2] nvarchar(3))
Insert #T1
select N'a',N'我的' union all
select N'b',N'你的' union all
select N'c',N'他的' union all
select N'd',N'大家的'
Go
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([F1] nvarchar(3),[OTHER] nvarchar(3))
Insert #T2
select N'a,b',N'027' union all
select N'c',N'961' union all
select N'a,c',N'091'
Go
Select a.[F1],列2=stuff((select ','+[F2] from #T1 where ','+a.[F1]+',' like '%,'+[F1]+',%' for xml path('')),1,1,''),a.[OTHER]
from #T2 a
drop table t1
Go
Create table t1([F1] nvarchar(1),[F2] nvarchar(3))
Insert t1
select N'a',N'我的' union all
select N'b',N'你的' union all
select N'c',N'他的' union all
select N'd',N'大家的'
Go
if not object_id('t2') is null
drop table t2
Go
Create table t2([F1] nvarchar(3),[OTHER] nvarchar(3))
Insert t2
select N'a,b',N'027' union all
select N'c',N'961' union all
select N'a,c',N'091'
Go
if object_id('f_hb')is not null drop function f_hb
go
create function f_hb(@id varchar(10))
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str=''
select @str=@str+','+[F2] from [t1] where charindex(','+[F1]+',',','+@id+',')>0
return stuff(@str,1,1,'')
end
go
select [F1],
[col2]=dbo.f_hb([F1]),
[OTHER]
from [t2]
/*
F1 col2 OTHER
---- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----
a,b 我的,你的 027
c 他的 961
a,c 我的,他的 091(3 個資料列受到影響)
*/
(
F1 varchar(10),
F2 varchar(20)
)
insert into T111 select 'a','我的'
insert into T111 select 'b','你的'
insert into T111 select 'c','他的'
insert into T111 select 'd','大家的'create table #T2
(
F1 varchar(10),
OTHER varchar(10)
)
insert into #T2 select 'a,b','027'
insert into #T2 select 'c','961'
insert into #T2 select 'a,c','091'declare @sql varchar(100)
set @sql=''
select @sql=@sql+T1.F1+',' from #T1 T1,#T2 T2 where charindex(T1.F1,T2.F1)>0
select @sqlcreate function dbo.FC_Str(@F1 varchar(10))
returns varchar(1000)
as
begin
declare @sql varchar(100)
set @sql=''
select @sql=@sql+','+F2 from T111 where charindex(','+F1+',',','+@F1+',')>0
return stuff (@sql,1,1,'')
endselect F1,dbo.FC_Str(F1),OTHER from #T2
F1 OTHER
---------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------
a,b 我的,你的 027
c 他的 961
a,c 我的,他的 091(3 行受影响)
go
--> -->
if not object_id(N'T1') is null
drop table T1
Go
Create table T1([F1] nvarchar(1),[F2] nvarchar(3))
Insert T1
select N'a',N'我的' union all
select N'b',N'你的' union all
select N'c',N'他的' union all
select N'd',N'大家的'
Go
if not object_id(N'T2') is null
drop table T2
Go
Create table T2([F1] nvarchar(3),[OTHER] nvarchar(3))
Insert T2
select N'a,b',N'027' union all
select N'c',N'961' union all
select N'a,c',N'091'
Go
create function F_ReplaceStr(@Str nvarchar(200))
returns nvarchar(200)
as
begin
set @Str=','+@Str+','
select @Str=replace(@Str,','+[F1]+',',','+[F2]+',') from T1 where @Str like '%,'+[F1]+',%'
return substring(@Str,2,len(@Str)-2)
endSelect a.[F1],列2=dbo.F_ReplaceStr(a.[F1]),a.[OTHER]
from T2 a
-- Author :SQL77(只为思齐老)
-- Date :2010-03-03 10:26:58
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB1
if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
go
create table #TB1([F1] varchar(1),[F2] varchar(6))
insert #TB1
select 'a','我的' union all
select 'b','你的' union all
select 'c','他的' union all
select 'd','大家的'
--> 测试数据:#tb2
if object_id('tempdb.dbo.#tb2') is not null drop table #tb2
go
create table #tb2([F1] varchar(50),[OTHER] varchar(3))
insert #tb2
select 'a,b','027' union all
select 'c','961' union all
select 'a,c','091'
--------------开始查询--------------------------
SELECT * INTO #T2 FROM #TB2
while exists(select 1 from #tb1 t1,#T2 t2 where charindex(','+t1.f1+',',','+t2.f1+',')>0)
begin
UPDATE T2 SET F1=replace(t2.f1,t1.f1,t1.f2)
from #T2 t2,#tb1 t1 where charindex(','+t1.f1+',',','+t2.f1+',')>0
endSELECT T.F1,T2.F1,T.OTHER FROM #T2 T2,#TB2 T WHERE T2.[OTHER]=T.[OTHER]
--DROP TABLE #T2
-- select * from #TB1
-- select * from #tb2
----------------结果----------------------------
/*
F1 F1 OTHER
-------------------------------------------------- -------------------------------------------------- -----
a,b 我的,你的 027
c 他的 961
a,c 我的,他的 091(所影响的行数为 3 行)
*/
--环境
create table t1
(
f1 varchar(2),
f2 varchar(10)
)
go
create table t2
(
f1 varchar(20),
other varchar(10)
)
go--数据
insert into t1 select 'a', '我的'
insert into t1 select 'b', '你的'
insert into t1 select 'c', '他的'
insert into t1 select 'd', '大家的'
goinsert into t2 select 'a,b', '027'
insert into t2 select 'c' , '961'
insert into t2 select 'a,c', '091'
go--sql server 2000
CREATE FUNCTION dbo.f_str(@f1 varchar(20))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = '' SELECT @r = @r + ',' + f2
FROM t1 a
WHERE charindex(','+a.f1+',',',' + @f1 + ',') > 0 RETURN STUFF(@r, 1, 1, '')
END
GO-- 调用函数
SELECt distinct b.f1, dbo.f_str(b.f1) as f2,b.other
FROM t1 a inner join t2 b on charindex(','+a.f1+',',',' + b.f1 + ',') > 0/* 结果a,b 我的,你的 027
a,c 我的,他的 091
c 他的 961
(3 行受影响)*/--sql server 2005
SELECT *
FROM(
SELECT DISTINCT f1,other
FROM t2
)A
OUTER APPLY(
SELECT
[f2]= STUFF(REPLACE(REPLACE(
(
SELECT f2 FROM t1 N
WHERE charindex(','+f1+',',',' + a.f1 + ',') > 0
FOR XML AUTO
), '<N f2="', ','), '"/>', ''), 1, 1, '')
)N--结果集
/*
a,b 027 我的,你的
a,c 091 我的,他的
c 961 他的
*/--删除环境
drop table t1
drop table t2
--环境
create table t1
(
f1 varchar(2),
f2 varchar(10)
)
go
create table t2
(
f1 varchar(20),
other varchar(10)
)
go--数据
insert into t1 select 'b', '你的'
insert into t1 select 'a', '我的'
insert into t1 select 'c', '他的'
insert into t1 select 'd', '大家的'
goinsert into t2 select 'a,b', '027'
insert into t2 select 'c' , '961'
insert into t2 select 'a,c', '091'
go--sql server 2000
CREATE FUNCTION dbo.f_str(@f1 varchar(20))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = '' SELECT @r = @r + ',' + f2
FROM t1 a
WHERE charindex(','+a.f1+',',',' + @f1 + ',') > 0 RETURN STUFF(@r, 1, 1, '')
END
GO-- 调用函数
SELECt distinct b.f1, dbo.f_str(b.f1) as f2,b.other
FROM t1 a inner join t2 b on charindex(','+a.f1+',',',' + b.f1 + ',') > 0/*
f1 f2 other
-------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------
a,b 你的,我的 027
a,c 我的,他的 091
c 他的 961(所影响的行数为 3 行)*/用函数的方法,顺序不能保证,如果加上ORDER BY 的话2000中不能连接字符串了,