Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:28+00:00 2026-06-12T13:23:28+00:00

Some folks using our software have gone ahead and done a database server move

  • 0

Some folks using our software have gone ahead and done a database server move not following our normal steps which accommodate the legacy portions of our software rending nulls = 0 in all databases. So, since they ignored using our protocols, we’re left with a lot of junk null data and a lot of queries for fieldname = 0 that fail because fieldname is null is true instead.

So, how would one go about constructing a stored procedure to revert all nulls (there really should be no nulls) to the default value in a SQL Server database?

I have almost no experience doing stored procedures and absolutely no experience outside of Oracle DBs in stored procedures so please explain what the code is doing if you can answer this with a code sample and if it’s impossible to run procedures over multiple tables, that’s good to know too, in that case I’ll just write a utility to do it (although I can already see that utility taking 9 years to run)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:23:30+00:00Added an answer on June 12, 2026 at 1:23 pm

    This will list all of the nullable columns in your tables:

    select tab.name, col.name 
    from sys.columns col join sys.tables tab on col.object_id = tab.object_id
    where tab.type = 'U' and col.is_nullable = 1
    

    You could potentially use a cursor to loop through each, executing some dynamic SQL to update the column definitions. But that could get quite nasty if you have different data types in each of the columns – are they all the same?

    Alternatively, you could update all of your SP queries to use:

    ISNULL(fieldname, 0) = 0
    

    to eliminate the NULL problem from that side – this will match on NULL -or- Zero.

    EDIT:

    Ok, assuming you want to update all INT, TINYINT, BIGINT and BIT columns, this script should do it. Use at your own risk though! Backup the DB and restore it as a copy and test it on there. Then, uncomment the table filter in the WHERE clause to test on a single table first. The EXECs are commented, only uncomment once you’ve tested it to screen.

    This script will do the following:

    1. UPDATE every NULL INT, TINYINT, BIGINT and BIT to Zero (false)
    2. ALTER every INT, TINYINT, BIGINT and BIT column to NOT NULL
    3. ADD a default constraint to every INT, TINYINT, BIGINT and BIT column to Zero (false).

    Note that 3 will fail if you already have a default constraint on the column. Remove it if you do not need the default value (i.e. you are explicitly inserting a value for every INSERT).

    DECLARE @table VARCHAR(MAX), @col VARCHAR(MAX), @type VARCHAR(MAX)
    DECLARE @q_update VARCHAR(MAX), @q_alter VARCHAR(MAX), @q_default VARCHAR(MAX)
    
    DECLARE c CURSOR FOR
    
        select tab.name, col.name, typ.name
        from sys.columns col
              join sys.types typ on col.system_type_id = typ.system_type_id
              join sys.tables tab on col.object_id = tab.object_id
    
        where tab.type = 'U' and col.is_nullable = 1 and typ.name IN ('int', 'tinyint', 'bigint', 'bit')
        --tab.name = 'sometable'
    
    OPEN c
    FETCH NEXT FROM c INTO @table, @col, @type
    WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT 'UPDATING ' + @table + '.' + @col + ' (' + @type + ')';
    
        SET @q_update = 'UPDATE [' + @table + '] SET [' + @col + '] = 0 WHERE [' + @col + '] IS NULL';
        PRINT @q_update;
        --EXEC(@q_update);
    
        PRINT '';
    
        SET @q_alter = 'ALTER TABLE [' + @table + '] ALTER COLUMN [' + @col + '] ' + @type + ' NOT NULL';
        PRINT @q_alter;
        --EXEC(@q_alter);
    
        PRINT '';
    
        SET @q_default = 'ALTER TABLE [' + @table + '] ADD CONSTRAINT ' + @table + '_' + @col + '_DF DEFAULT 0 FOR [' + @col + ']'
        PRINT @q_default;
        --EXEC(@q_default);
    
        PRINT '----';
    
        FETCH NEXT FROM c INTO @table, @col, @type
    END
    CLOSE c
    DEALLOCATE c
    

    Note: I’ve used cursors for clarity. Feel free to look at @JamesCurtis link if you really don’t want to use them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation and I'm expecting some expert advise from SO folks.
We have moved to Visual Studio 2010 but our TFS folks are not ready
I've been looking at various RIA and have noticed some folks using or requesting
HI folks, I have a .net application (vb.net) and I'm using the ajax control
Folks, I am creating an installer project in Visual Studio. This is done using
Using .Net 4.0 and SQL Server 2008 R2 I have been looking into utilizing
In a very old code base, we have some build settings for our visual
look at this image folks, instead of using overflow:hidden there is still some text
Folks, I have some HTML generated from my WSDL and XSD files for a
Folks, My Issue Why is an HSQLDB file-based database I have pre-populated with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.