sql 使用 COUNT 设置变量

2021-09-10 00:00:00 tsql sql-server

我正在尝试对我的服务器进行简单查询,并希望将结果存储在变量 @times 中.

I am trying to make a simple query to my server and want the result to be stored in the variable @times.

DECLARE @times int

SET @times = SELECT COUNT(DidWin)as "I Win"
FROM thetable
WHERE DidWin = 1 AND Playername='Me'

IntelliSense 说 Select 附近的语法错误

IntelliSense says Wrong syntax near Select

推荐答案

你只需要在你的选择周围加上括号:

You just need parentheses around your select:

SET @times = (SELECT COUNT(DidWin) FROM ...)

或者你可以这样做:

SELECT @times = COUNT(DidWin) FROM ...

相关文章