是否有可能构造SQL来连接列值
多行吗?
举例如下:
表一个
PID
A
B
C
表B
PID SEQ Desc
A 1 Have
A 2 a nice
A 3 day.
B 1 Nice Work.
C 1 Yes
C 2 we can
C 3 do
C 4 this work!
SQL的输出应该是-
PID Desc
A Have a nice day.
B Nice Work.
C Yes we can do this work!
所以基本上输出表的Desc列是来自表B的SEQ值的连接?
SQL有什么帮助吗?
在想要连接的选择中,调用SQL函数。
例如:
select PID, dbo.MyConcat(PID)
from TableA;
然后对于SQL函数:
Function MyConcat(@PID varchar(10))
returns varchar(1000)
as
begin
declare @x varchar(1000);
select @x = isnull(@x +',', @x, @x +',') + Desc
from TableB
where PID = @PID;
return @x;
end
Function Header语法可能是错误的,但原理是正确的。