动态显示论坛评论、

用sql中的函数来查询指定的ID的文章及评论。

/*
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()
)

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
*/
select
    Title,[Content],CreateDate,dbo.fn_GetAllComments(PostID) as AllComments
from Post

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值