简体   繁体   中英

SQL Loop through tables and columns to find which columns are NOT empty

I created a temp table #test containing 3 fields: ColumnName, TableName, and Id. I would like to see which rows in the #test table (columns in their respective tables) are not empty? Ie, for every column name that i have in the ColumnName field, and for the corresponding table found in the TableName field, i would like to see whether the column is empty or not. Tried some things (see below) but didn't get anywhere. Help, please.

declare @LoopCounter INT = 1, @maxloopcounter int, @test varchar(100),
        @test2 varchar(100), @check int
set @maxloopcounter =  (select count(TableName) from #test)

while @LoopCounter <= @maxloopcounter
begin
    DECLARE @PropIDs TABLE (tablename varchar(max), id int )

    Insert into @PropIDs (tablename, id) 
    SELECT [tableName], id FROM #test  
    where id = @LoopCounter 

    set @test2 = (select columnname from #test where id = @LoopCounter)

    declare @sss varchar(max)
    set @sss = (select tablename from @PropIDs where id = @LoopCounter)

    set @check = (select count(@test2) 
                  from (select tablename 
                        from @PropIDs 
                        where id = @LoopCounter) A
                 )

    print @test2
    print @sss
    print @check

    set @LoopCounter = @LoopCounter + 1
end

为了在@Check=查询中使用变量作为列名和表名,您需要使用Dynamic SQL

There is most likely a better way to do this but I cant think of one off hand. Here is what I would do.

  1. Use the select and declare a cursor rather than a while loop as you have it. That way you dont have to count on sequential id's. The cursor would fetch fields columnname, id and tablename

  2. In the loop build a dynamic sql statement

    Set @Sql = 'Select Count(*) Cnt Into #Temp2 From ' + TableName + ' Where ' + @columnname + ' Is not null And ' + @columnname <> '''''

  3. Exec(@Sql)

  4. Then check #Temp2 for a value greater than 0 and if this is what you desire you can use the @id that was fetched to update your #Temp table. Putting the result into a scalar variable rather than a temp table would be preferred but cant remember the best way to do that and using a temp table allows you to use an update join so it would well in my opinion.

https://www.mssqltips.com/sqlservertip/1599/sql-server-cursor-example/ http://www.sommarskog.se/dynamic_sql.html

Found a way to extract all non-empty tables from the schema, then just joined with the initial temp table that I had created.

select A.tablename, B.[row_count]

from (select * from #test) A

left join

(SELECT r.table_name, r.row_count, r.[object_id]
FROM sys.tables t
INNER JOIN (
    SELECT OBJECT_NAME(s.[object_id]) table_name, SUM(s.row_count) row_count, s.[object_id]
    FROM sys.dm_db_partition_stats s
    WHERE s.index_id in (0,1)
    GROUP BY s.[object_id]
) r on t.[object_id] = r.[object_id]
WHERE r.row_count > 0 ) B
on A.[TableName] = B.[table_name]

WHERE ROW_COUNT > 0
order by b.row_count desc

How about this one - bitmask computed column checks for NULLability. Value in the bitmask tells you if a column is NULL or not. Counting base 2.

    CREATE TABLE FindNullComputedMask
        (ID int
        ,val int
        ,valstr varchar(3)
        ,NotEmpty as 
            CASE WHEN ID IS NULL THEN 0 ELSE 1 END 
              | 
            CASE WHEN val IS NULL THEN 0 ELSE 2 END 
              | 
            CASE WHEN valstr IS NULL THEN 0 ELSE 4 END
        )

    INSERT FindNullComputedMask 
    SELECT 1,1,NULL

    INSERT FindNullComputedMask 
    SELECT NULL,2,NULL

    INSERT FindNullComputedMask 
    SELECT 2,NULL, NULL

    INSERT FindNullComputedMask
    SELECT 3,3,3

    SELECT *
    FROM FindNullComputedMask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM