declare @a table(name1 int,name2 int,name3 varchar(20)) insert @a select 1,2,'t' declare @b table(name1 int,name2 varchar(20)) insert @b select 1,'aaa' union all select 2,'bbb' declare @c table(name1 varchar(20),name2 varchar(20)) insert @c select 't','ccc'declare @str1 varchar(200) set @str1='' select @str1=cast(name1 as varchar)+','+cast(name2 as varchar)+','+name3 from @a print @str1 declare @str2 varchar(200) set @str2='' select @str2=@str2+' '+name2 from @b b where charindex(cast(b.name1 as varchar),@str1)>0 select @str2=@str2+' '+name2 from @c c where charindex(c.name1,@str1)>0 print @str2 /*(所影响的行数为 1 行) (所影响的行数为 2 行) (所影响的行数为 1 行)1,2,t aaa bbb ccc */
谢谢楼上的 不过没看懂 能不能写个简单了 就一句SQL搞定不用Print
不是我没表达清楚意思tableA1 2 3table B 1 aaa 2 bbbtable C 3 cccc结果: aaa bbb ccc
select DISTINCT(SELECT tableB.B FROM TABLEB WHERE tableB.A = 1), (SELECT TABLEB.B FROM tableB WHERE tableB.A = 2 ), (SELECT TABLEC.B FROM tableC WHERE tableC.A = 'T') FROM tableA,tableB,tableC
insert @a
select 1,2,'t'
declare @b table(name1 int,name2 varchar(20))
insert @b
select 1,'aaa'
union all
select 2,'bbb'
declare @c table(name1 varchar(20),name2 varchar(20))
insert @c
select 't','ccc'declare @str1 varchar(200)
set @str1=''
select @str1=cast(name1 as varchar)+','+cast(name2 as varchar)+','+name3 from @a
print @str1
declare @str2 varchar(200)
set @str2=''
select @str2=@str2+' '+name2 from @b b where charindex(cast(b.name1 as varchar),@str1)>0
select @str2=@str2+' '+name2 from @c c where charindex(c.name1,@str1)>0
print @str2
/*(所影响的行数为 1 行)
(所影响的行数为 2 行)
(所影响的行数为 1 行)1,2,t
aaa bbb ccc
*/
1 aaa
2 bbbtable C
3 cccc结果: aaa bbb ccc
(SELECT TABLEB.B FROM tableB WHERE tableB.A = 2 ),
(SELECT TABLEC.B FROM tableC WHERE tableC.A = 'T')
FROM tableA,tableB,tableC