-- Author: Stone
-- Create date: 2008-7-17
-- Description: 利用自定义函数Group By 数据 Exmaple:
/*需求:
将 A 1 转成 A 1,3,5
B 2 B 2,4
A 3
B 4
A 5
*/
Create FUNCTION dbo.GetMenth
(
@p1 nvarchar(1000)
)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @Result varchar(8000)
set @Result= ''
select @Result=@Result+','+rtrim(FK_CC) from CCAssignmentHistory
where FK_Client=@p1
SELECT @Result =stuff(@Result,1,1,'')
RETURN @Result
END
GO
if exists(select * from sysobjects where name='#temp') drop table #temp
select dbo.GetMenth(FK_Client) as Oldcc,FK_Client into #temp from CCAssignmentHistory group by FK_Client
Update client set Oldcc=null
Update Client set Oldcc=tbl.Oldcc
from
(
select FK_Client,Oldcc from #temp
)tbl
where tbl.FK_Client=Client.PKID
select oldcc from client where oldcc is not null
Drop table #temp
drop function dbo.GetMenth