SQL case when set variable

天涯浪子 提交于 2019-12-11 11:53:28

问题


I am trying to set 2 declared variable with case when blocks

Here's my code:

DECLARE @like bit,@dislike bit
if exists
( select * 
    ,@like = (CASE WHEN likeordislike = 1 THEN 'true' ELSE 'false' END)
    ,@dislike=(CASE WHEN likeordislike = 0 THEN 'true' ELSE 'false' END)
  from likeordislike
)

But when I execute query throws errors:

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '='.

Everything is ok? Couldn't understand


回答1:


if (select count(*) from likeordislike where user = @user and comment_id = @comment_id) = 0
begin
  insert into likeordislike etc
end 
else
  update likeordislike etc
end


来源:https://stackoverflow.com/questions/15935334/sql-case-when-set-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!