SQL返回值作为参数

时间:2013-04-11 09:24:40

标签: sql return-value scope-identity

我昨天在这里问了一个问题question

现在我改变了我的代码,如下所示

declare @like bit, @dislike bit

IF EXISTS (select * from likeordislike 
           where comment_id = @comment_id and likevar = 1 and dislikevar = 0)
BEGIN
    SET @like    = 'true'
    SET @dislike = 'false'
END
ELSE IF EXISTS (select * from likeordislike 
                where comment_id = @comment_id and likevar = 0 and dislikevar = 1)
BEGIN
    SET @like    = 'false'
    SET @dislike = 'true'
END
ELSE
BEGIN
    SET @like = 'false'
    SET @dislike ='false'
END

SELECT     
    comment_id, comment_post_id, comment_user_id, 
    comment_text, comment_like, comment_dislike, 
    comment_reply_to_id, comment_date,
    @like as begendi,
    @dislike as begenmedi
FROM         
    comment 
ORDER BY 
    comment_date DESC

我正在尝试选择评论或评论喜欢或不喜欢状态。

我在我的asp.net网页上使用了一个数据列表。我需要每个评论的返回值。我必须提供@comment_id参数,此查询会为每个注释(begendi = 1, begenmedi = 0)返回相同的结果。我怎么解决这个问题?

需要

ScopeIdentity或返回值?谢谢(我有两个表1-comment表,2-likeordislike表有comment_id,user_id,likevar,dislikevar和eventdate列)

select * 
from likeordislike 
where comment_id = @comment_id and likevar = 0 and dislikevar = 1

1 个答案:

答案 0 :(得分:1)

我不确定你在这里想要实现什么,但我认为你可以在不使用程序方法和简单地使用连接的情况下实现你所追求的结果:

SELECT  comment_id, 
        comment_post_id, 
        comment_user_id, 
        comment_text, 
        comment_like, 
        comment_dislike, 
        comment_reply_to_id, 
        comment_date,
        begendi = CAST(CASE WHEN LikeOrDislike.Likes > 0 OR LikeOrDislike.Dislikes = 0 THEN 1 ELSE 0 END AS BIT),
        begenmedi = CAST(CASE WHEN LikeOrDislike.Dislikes > 0 AND LikeOrDislike.Likes = 0 THEN 1 ELSE 0 AS BIT)
FROM    Comment
        LEFT JOIN 
        (   SELECT  Comment_ID,
                    Likes = COUNT(CASE WHEN LikeVar = 1 AND DislikeVar = 0 THEN 1 END),
                    Dislikes = COUNT(CASE WHEN LikeVar = 0 AND DislikeVar = 1 THEN 1 END),
                    Other = COUNT(CASE WHEN LikeVar = DislikeVar THEN 1 END)
            FROM    LikeOrDislike
            GROUP BY Comment_ID
        ) LikeOrDislike
            ON Comment.Comment_ID = LikeOrDislike.Comment_ID
ORDER BY comment_date DESC