如何针对表的所有列检查条件?

时间:2017-09-12 11:50:55

标签: sql sql-server

我有一个包含30多列的表(都是varchar)。我需要列出所有包含空白的列,即' '价值观。

我尝试使用'coalesce',但它仅用于NULL。

6 个答案:

答案 0 :(得分:3)

以下查询将为您提供表中可能包含null''值的所有列。 编写它是为了让你可以为数据库中的所有表运行它,但是你可以将它限制在一个表中,就像我为这个特定的例子所做的那样,检查一个名为testingNulls的表:

--two variables needed for table name and column name, when looping through all tables
declare @table varchar(255), @col varchar(255), @sql varchar(max)

--this will be used to store the result, to have one result set instead of one row per each cursor cycle
if object_id('tempdb..#nullcolumns') is not null drop table #nullcolumns
create table #nullcolumns (tablename varchar(255), columnname varchar(255))

declare getinfo cursor for
select t.name tablename, c.name 
from sys.tables t join sys.columns c on t.object_id = c.object_id 
where t.name = 'testingnulls' --here the condition for the table name
open getinfo
fetch next from getinfo into @table, @col
while @@fetch_status = 0
begin
  select @sql = 'if exists (select top 1 * from [' + @table + '] where [' + @col + '] is null or [' + @col + '] like '''' ) begin insert into #nullcolumns select ''' + @table + ''' as tablename, ''' + @col + ''' as all_nulls end'
  print(@sql)
  exec(@sql)
  fetch next from getinfo into @table, @col
end
close getinfo
deallocate getinfo

--this should be the result you need:
select * from #nullcolumns

您可以看到一个有效的例子here。我希望这就是你所需要的。

答案 1 :(得分:2)

列出某些记录中包含空格的所有列?您将使用每列查询并使用UNION ALL收集结果:

select 'COL1' where exists (select * from mytable where col1 like '% %')
union all
select 'COL2' where exists (select * from mytable where col2 like '% %')
union all
...
union all
select 'COL30' where exists (select * from mytable where col30 like '% %');

答案 2 :(得分:2)

如果您想要 select * from [your_table_name] where [col1] = '' and [col2] = ''..... ,请使用如下的动态SQL查询。

<强>查询

declare @sql as varchar(max);
select @sql = 'select * from [your_table_name] where ' 
    + stuff((
            select ' and [' + [column_name] + '] = ' + char(39) + char(39)
            from information_schema.columns
            where table_name = 'your_table_name'
            for xml path('')
    ) 
    , 1, 5, ''
);

exec(@sql);

更新

否则,如果要列出具有空值的列名,则可以使用以下动态sql查询。

<强>查询

declare @sql as varchar(max);
select @sql = stuff((
        select ' union all select ' + [column_name] + ' as [col1], ' 
        + char(39) + [column_name] + char(39) + ' as [col2]'
        + ' from your_table_name'
        from information_schema.columns
        where table_name = 'your_table_name'
        for xml path('')
    )
    , 1, 11, ''
); 

set @sql = 'select distinct t.col2 as [blank_cols] from(' + @sql 
+ ')t 
where coalesce(ltrim(rtrim(t.col1)), ' + char(39) + char(39) + ') = ' 
+ char(39) + char(39) + ';';

exec(@sql);

<强> Find a demo here

但我仍然不确定这是你想要的。

答案 3 :(得分:1)

你没有太多选择,只能指定where子句中的所有列

WHERE COL1 = '' AND COL2 = '' AND COL3 = '' AND . . .

或者您可以使用动态SQL来构建查询,但这不是一个容易的路径

答案 4 :(得分:1)

如果您想计算具有&#39;&#39;表中的值(不是每行),然后使用以下

SELECT max(CASE WHEN col1 = '' THEN 1 ELSE 0 END) +
       max(CASE WHEN col2 = '' THEN 1 ELSE 0 END) +
       max(CASE WHEN col3 = '' THEN 1 ELSE 0 END) +
       ...
FROM t

demo

答案 5 :(得分:0)

我创建了一个动态SQL脚本,您只需提供表名即可使用该脚本 这是

declare @sql nvarchar(max)
declare @table sysname = 'ProductAttributes'

select @sql = 
    'select * from ' + @table + ' where ' +
    string_agg('[' + name + '] = '' '' ', ' and ')
from sys.columns 
where object_id = OBJECT_ID(@table)

select @sql

exec sp_executesql @sql

不幸的是,SQL string concatenation String_Agg function是SQL Server 2017的新功能 但也可以使用SQL XML Path to concatenate WHERE子句片段

SELECT  @sql = 'select * from ' + @table + ' where ' +
  STUFF(
    (
    SELECT
      ' and ' + '[' + [name] + '] = '' '' '
    from sys.columns 
    where object_id = OBJECT_ID(@table)
    FOR XML PATH(''),TYPE
    ).value('.','VARCHAR(MAX)'
    ), 1, 5, ''
  )

select @sql as sqlscript
exec sp_executesql @sql