Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Virtual Lab: Setting up Database Mail on SQL Server 2008 R2SQL Server and the Auto Close Setting »
    comments

    This post is based on a question I answered earlier today, someone wanted to check all columns that allow NULL for NULL values in all tables. The reason people might want to do this check is that they want to make all the columns not nullable in a database (after all we all know that developers hate NULLs).

    The Stored Procedure below is based on the code that George Mastros wrote for the following blog post: Search all columns in all the tables in a database for a specific value

    1. CREATE PROCEDURE FindColumnsWithNulls
    2. AS
    3. SET NOCOUNT ON
    4.  
    5. DECLARE @Temp TABLE(RowId INT IDENTITY(1,1),
    6.                     SchemaName sysname,
    7.                     TableName sysname,
    8.                     ColumnName SysName,
    9.                     DataType VARCHAR(100),
    10.                     DataFound BIT)
    11.  
    12.     --grab the columns that we care about
    13.     INSERT  INTO @Temp(TableName,SchemaName, ColumnName, DataType)
    14.     SELECT  C.Table_Name,C.TABLE_SCHEMA, C.Column_Name, C.Data_Type
    15.     FROM    Information_Schema.Columns AS C
    16.             INNER Join Information_Schema.Tables AS T
    17.                 ON C.Table_Name = T.Table_Name
    18.         AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
    19.     WHERE   Table_Type = 'Base Table'  --only tables, no views
    20.     AND C.IS_NULLABLE = 'YES'  --obviously only check nullable columns
    21.            
    22.  
    23.  
    24. DECLARE @i INT
    25. DECLARE @MAX INT
    26. DECLARE @TableName sysname
    27. DECLARE @ColumnName sysname
    28. DECLARE @SchemaName sysname
    29. DECLARE @SQL NVARCHAR(4000)
    30. DECLARE @PARAMETERS NVARCHAR(4000)
    31. DECLARE @DataExists BIT
    32. DECLARE @SQLTemplate NVARCHAR(4000)
    33.  
    34. SELECT  @SQLTemplate = 'If Exists(Select 1
    35.                                          From   ReplaceTableName
    36.                                          Where  [ReplaceColumnName] IS NULL)
    37.                                     Set @DataExists = 1
    38.                                 Else
    39.                                     Set @DataExists = 0',
    40.         @PARAMETERS = '@DataExists Bit OUTPUT',
    41.         @i = 1
    42.  
    43. SELECT @i = 1, @MAX = MAX(RowId)
    44. FROM   @Temp
    45.  --loop over all the columns
    46. WHILE @i <= @MAX
    47.     BEGIN
    48.         --change the place holder with the real name
    49.         SELECT  @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName',
    50.                        QUOTENAME(SchemaName) + '.' +
    51.                        QUOTENAME(TableName)), 'ReplaceColumnName', ColumnName)
    52.         FROM    @Temp
    53.         WHERE   RowId = @i
    54.  
    55.  
    56.         -- PRINT @SQL -- poor man's debugger  :-)
    57.  
    58.         EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @DataExists = @DataExists OUTPUT
    59.  
    60.         --update result table if a NULL is found
    61.         IF @DataExists =1
    62.             UPDATE @Temp SET DataFound = 1 WHERE RowId = @i
    63.  
    64.         SET @i = @i + 1
    65.     END
    66.  
    67. --Report to the user how many columns have NULLS
    68. SELECT  SchemaName,TableName, ColumnName,DataType
    69. FROM    @Temp
    70. WHERE   DataFound = 1

    You can call the code like this

    1. exec FindColumnsWithNulls

    You will get a 'report' that lists SchemaName, TableName, ColumnName and DataType

    This proc does not tell you how many NULLS you have in a column, it will just report that the column has at least one NULL value. With the output from the proc, it is pretty easy for you to find how many NULLS there are and then update the value to something that is not NULL

    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2425 views
    Instapaper

    2 comments

    Comment from: David Hay [Visitor]
    David Hay Fantastic, just what I needed! Thank you!
    08/20/10 @ 08:51
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Interesting, I somehow missed it. I tackle the same problem (a bit differently) in my Interesting T-SQL problems blog.
    10/07/10 @ 16:13

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)