create table Post ( [PostID] int identity(1,1) primary key not null, [Title] nvarchar(50), [Content] text, [CreateDate] datetime default getdate() )
create table Comments ( [CommentID] int identity(1,1) primary key not null, [PostID] int, [Content] text, [CreateDate] datetime default getdate() )
给这两个表添加一些测试数据
1 2 3 4
insert into Post select '钓鱼岛是中国的吗?','钓鱼岛是中国的',getdate() insert into Comments select 1,'绝对是',getdate() insert into Comments select 1,'必须是的',getdate() insert into Comments select 1,'谁说不是呢',getdate()
create FUNCTION fn_GetAllComments(@PostID int) RETURNS NVARCHAR(4000) AS BEGIN DECLARE @result VARCHAR(4000) SET @result='' DECLARE getAllComments CURSOR FOR select CommentID from Comments where PostID=@PostID OPEN getAllComments DECLARE @ID SYSNAME FETCH FROM getAllComments INTO @ID WHILE @@fetch_status=0 BEGIN SET @result=@result+(select convert(nvarchar(20),CreateDate,120) from Comments where CommentID=@ID)+':'+ (select cast([Content] as nvarchar(4000)) from Comments where CommentID=@ID)+';' FETCH FROM getAllComments INTO @ID END CLOSE getAllComments SET @result= substring(@result,0,len(@result)) DEALLOCATE getAllComments RETURN @result END
现在写SQL语句来测试一下结果
1 2 3
select Title,[Content],CreateDate,dbo.fn_GetAllComments(PostID) as AllComments from Post