SQL 计算 NULL 单元格

发布于 2024-08-30 00:19:14 字数 315 浏览 1 评论 0原文

我有以下问题。我在数据库中有一个表,有很多列。我可以执行不同类型的选择查询,例如,显示满足条件的每条记录:

  • 名称以 _t0 结尾的列中的所有单元格
  • 名称以 _t1 结尾的列中的所有单元格
  • ...

获取列列表我使用信息模式形成查询。

现在,问题是:每个查询返回一条记录,其中包含大表列的子集。这意味着我可以获得一行(全部!)NULL。如何要求我的查询拒绝此类行,而不必明确输入列名(即通过说其中 col_1 不为空,col_2 不为空...)?是否可以?

提前致谢!!!

九月

I have the following problem. I have a table in a db, with many columns. I can do different kind of select queries, to show, for example, for each record that satisfies a condition:

  • all cells from columns with names ending in _t0
  • all cells from columns with names ending in _t1
  • ...

To get the column lists to form the queries I use the information schema.

Now, the problem: each query returns a record with a subset of the columns of the big table. This means that I can get a row of (all!) NULLs. How can I ask my query to reject such rows without having to type in explicitely the column names (i.e. by saying where col_1 is not null, col_2 is not null...)? Is it possible?

Thanks in advance!!!

Sep

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

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

发布评论

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

评论(1

浮生未歇 2024-09-06 00:19:14

我一直在寻找同样的东西,但我认为这是不可能的。

唯一合适的解决方案似乎是为它们命名,来自 Bytes.com

SELECT
CASE WHEN patientid IS NULL THEN 1 ELSE 0 END+
CASE WHEN age IS NULL THEN 1 ELSE 0 END+
CASE WHEN weight IS NULL THEN 1 ELSE 0 END+
CASE WHEN height IS NULL THEN 1 ELSE 0 END+
CASE WHEN race IS NULL THEN 1 ELSE 0 END
FROM tblPatientDemographics

当然,这并不有趣,所以我编写了一些动态 SQL 来为我做到这一点。不太优雅,但我不确定是否有更好的方法。

DECLARE @tableName SYSNAME
DECLARE @sql VARCHAR(MAX)
DECLARE @sqltail VARCHAR(MAX)
-- loop through all the fields in a table.
DECLARE @colName SYSNAME
SET @tableName = 'YourTableName'
DECLARE crsCol CURSOR FORWARD_ONLY READ_ONLY
FOR
    SELECT  name
    FROM    syscolumns
    WHERE   id = OBJECT_ID(@tableName)
    ORDER BY colid
OPEN crsCol
SET @sql = ''
SET @sqltail = 'FROM [' + @tableName + ']'
FETCH NEXT FROM crsCol INTO @colName
WHILE @@FETCH_STATUS = 0 
    BEGIN
        IF @sql = ''
            SET @sql = 'SELECT /* Add Your PK Fields here, */ NullCount = '
        ELSE 
            SET @sql = @sql + '+ '

        SET @sql = @sql + 'CASE WHEN [' + @colName + '] IS NULL THEN 1 ELSE 0 END '

        FETCH NEXT FROM crsCol INTO @colName
    END
CLOSE crsCol
DEALLOCATE crsCol
PRINT (@sql + @sqltail)
EXEC(@sql + @sqltail)

I've been looking for the same, and I don't think it's possible.

The only decent solution seems to be to name them, from Bytes.com:

SELECT
CASE WHEN patientid IS NULL THEN 1 ELSE 0 END+
CASE WHEN age IS NULL THEN 1 ELSE 0 END+
CASE WHEN weight IS NULL THEN 1 ELSE 0 END+
CASE WHEN height IS NULL THEN 1 ELSE 0 END+
CASE WHEN race IS NULL THEN 1 ELSE 0 END
FROM tblPatientDemographics

Of course that's no fun, so I wrote some dynamic SQL to do this for me. Hardly elegant, but I'm not sure there's a better way.

DECLARE @tableName SYSNAME
DECLARE @sql VARCHAR(MAX)
DECLARE @sqltail VARCHAR(MAX)
-- loop through all the fields in a table.
DECLARE @colName SYSNAME
SET @tableName = 'YourTableName'
DECLARE crsCol CURSOR FORWARD_ONLY READ_ONLY
FOR
    SELECT  name
    FROM    syscolumns
    WHERE   id = OBJECT_ID(@tableName)
    ORDER BY colid
OPEN crsCol
SET @sql = ''
SET @sqltail = 'FROM [' + @tableName + ']'
FETCH NEXT FROM crsCol INTO @colName
WHILE @@FETCH_STATUS = 0 
    BEGIN
        IF @sql = ''
            SET @sql = 'SELECT /* Add Your PK Fields here, */ NullCount = '
        ELSE 
            SET @sql = @sql + '+ '

        SET @sql = @sql + 'CASE WHEN [' + @colName + '] IS NULL THEN 1 ELSE 0 END '

        FETCH NEXT FROM crsCol INTO @colName
    END
CLOSE crsCol
DEALLOCATE crsCol
PRINT (@sql + @sqltail)
EXEC(@sql + @sqltail)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文