合并分拆的问题
[Quote=引用 1 楼 roy_88 的回复:]
合并分拆
http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html
[/Quote]
if object_id('Tab') is not null
drop table Tab
Go
Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
go
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
--select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
select @S=@S+','+Col2 from Tab where Col1=@Col1 ------ 看这里(我自己做的修改)
return @S
end
go
Select Col1,Col2=dbo.F_Str(Col1) from Tab
go
我的问题是:
1. 为什么这样修改后结果都是为空呢,isnull(@S+',','')这个不就是只判断当 @S+','为空时返回空,那怎么会产生那种结果呢?
2. 函数里面有循环语句那为什么能将COL1相同的COL2值都相加呢?
菜鸟请高手帮下