将 select count(*) 值保存为整数 (SQL Server)

发布于 2024-08-23 21:15:43 字数 343 浏览 7 评论 0原文

我在使用此语句时遇到了一些问题,这无疑是由于我不知道此 select 语句返回的内容:

declare @myInt as INT
set @myInt = (select COUNT(*) from myTable as count)

if(@myInt <> 0) 
begin
   print 'there's something in the table'
end

myTable 中有记录,但当我运行上面的代码时, print 语句永远不会运行。进一步检查表明,在上述赋值之后 myInt 实际上为零。我确信我错过了一些东西,但我假设选择计数会返回一个我可以在上面使用的标量?

I'm having some trouble with this statement, owing no doubt to my ignorance of what is returned from this select statement:

declare @myInt as INT
set @myInt = (select COUNT(*) from myTable as count)

if(@myInt <> 0) 
begin
   print 'there's something in the table'
end

There are records in myTable, but when I run the code above the print statement is never run. Further checks show that myInt is in fact zero after the assignment above. I'm sure I'm missing something, but I assumed that a select count would return a scalar that I could use above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪都笑了 2024-08-30 21:15:43

如果 @myInt 为零,则意味着表中没有行:如果根本没有设置,它将为 NULL。

COUNT 将始终返回一行,即使表中没有行。

2012 年 4 月编辑:我的答案中描述了此规则:COUNT(*) 总是返回结果吗?

您的计数/分配是正确的,但可以是任何一种方式:

select @myInt = COUNT(*) from myTable
set @myInt = (select COUNT(*) from myTable)

但是,如果您只是查找行是否存在,(NOT) EXISTS 更有效:

IF NOT EXISTS (SELECT * FROM myTable)

If @myInt is zero it means no rows in the table: it would be NULL if never set at all.

COUNT will always return a row, even for no rows in a table.

Edit, Apr 2012: the rules for this are described in my answer here:Does COUNT(*) always return a result?

Your count/assign is correct but could be either way:

select @myInt = COUNT(*) from myTable
set @myInt = (select COUNT(*) from myTable)

However, if you are just looking for the existence of rows, (NOT) EXISTS is more efficient:

IF NOT EXISTS (SELECT * FROM myTable)
冰之心 2024-08-30 21:15:43
select @myInt = COUNT(*) from myTable
select @myInt = COUNT(*) from myTable
深海少女心 2024-08-30 21:15:43
Declare @MyInt int
Set @MyInt = ( Select Count(*) From MyTable )

If @MyInt > 0
Begin
    Print 'There''s something in the table'
End

我不确定这是否是您的问题,但您必须使用第二个单引号来转义 print 语句中的单引号。虽然您可以使用 SELECT 来填充变量,但在我看来,使用您在此处所做的 SET 就很好而且更清晰。此外,您可以保证 Count(*) 永远不会返回负值,因此您只需检查它是否大于零。

Declare @MyInt int
Set @MyInt = ( Select Count(*) From MyTable )

If @MyInt > 0
Begin
    Print 'There''s something in the table'
End

I'm not sure if this is your issue, but you have to esacpe the single quote in the print statement with a second single quote. While you can use SELECT to populate the variable, using SET as you have done here is just fine and clearer IMO. In addition, you can be guaranteed that Count(*) will never return a negative value so you need only check whether it is greater than zero.

感受沵的脚步 2024-08-30 21:15:43

[更新]——嗯,我自己的愚蠢提供了这个问题的答案。事实证明,我在运行 select COUNT 语句之前从 myTable 中删除了记录。

我是怎么做到的却没有注意到?很高兴你问了。我一直在测试 sql 单元测试平台 (tsqlunit,如果您有兴趣) 并作为其中一个测试的一部分我运行了截断表语句,然后是上面的。单元测试结束后,一切都会回滚,记录又回到 myTable 中。这就是为什么我在测试之外获得了创纪录的计数。

对不起大家...感谢您的帮助。

[update] -- Well, my own foolishness provides the answer to this one. As it turns out, I was deleting the records from myTable before running the select COUNT statement.

How did I do that and not notice? Glad you asked. I've been testing a sql unit testing platform (tsqlunit, if you're interested) and as part of one of the tests I ran a truncate table statement, then the above. After the unit test is over everything is rolled back, and records are back in myTable. That's why I got a record count outside of my tests.

Sorry everyone...thanks for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文