SQL Server 子查询不返回任何记录 [英] SQL Server Subquery Returning No Records

查看:52
本文介绍了SQL Server 子查询不返回任何记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的子查询:

I have what should be a very simple subquery:

DECLARE @count TABLE (count1 INT, count2 INT)

SELECT
    (SELECT COUNT(*) FROM Bird) AS count1,
    (SELECT COUNT(*) FROM Fish) AS count2
FROM @count

  • 当我运行 SELECT COUNT(*) FROM Bird 时,我得到了 10.
  • 当我运行 SELECT COUNT(*) FROM Fish 时,我得到 5 个返回值.
  • 运行上述查询时,返回的记录为零.
    • When I run SELECT COUNT(*) FROM Bird I get 10 back.
    • When I run SELECT COUNT(*) FROM Fish I get 5 returned.
    • When I run the above query, I get zero records returned.
    • 我错过了什么吗?我看过多个不同的教程,他们都说要做我正在做的事情.

      Am I missing something? I've seen multiple different tutorials and they all say to do pretty much exactly what I am doing.

      推荐答案

      我假设您正在尝试将两个计数插入到您声明的表中.如果这是真的,您将缺少 INSERT INTO 并且有一个 FROM @count 您不需要...

      I assume, that you are trying to insert the two counts into your declared table. If this is true you are missing an INSERT INTO and there is one FROM @count you don't need...

      在本例中,我使用两个计数(在其他表上)将一行插入到 @count 中,您可以在最后一步中选择:

      In this example I use two count (on other tables) to insert one row into @count, which you can select in the last step:

      declare @count table (count1 int, count2 int);
      
      INSERT INTO @count
      select (SELECT COUNT(*) FROM sys.all_columns)
            ,(SELECT COUNT(*) FROM sys.tables);
      
      SELECT * FROM @count;
      

      这篇关于SQL Server 子查询不返回任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆