Find All Rows With Null Value(s) in Any Column

后端 未结 3 1833
梦毁少年i
梦毁少年i 2020-12-06 00:16

I\'m trying to create a query that will return all the rows that have a null value across all but 1 column. Some rows will have more than one null entry somewhere. There i

3条回答
  •  情书的邮戳
    2020-12-06 00:53

    Depending on which RDBMS you're using, I think your only option (rather than explicitly saying WHERE col1 IS NULL and col2 IS NULL and col3 IS NULL ...) would be to use Dynamic SQL.

    For example, if you want to get all the column names from a SQL Server database, you could use something like this to return those names:

    SELECT
         name
    FROM
         sys.columns
    WHERE
         object_id = OBJECT_ID('DB.Schema.Table')
    

    You could use FOR XML to create your WHERE clause:

    SELECT Name + ' IS NULL AND ' AS [text()]
    FROM sys.columns c1
    WHERE     object_id = OBJECT_ID('DB.Schema.Table')
    ORDER BY Name
    FOR XML PATH('')
    

    Hope this helps get you started.

    Good luck.

提交回复
热议问题