2

I need to update all null values of a specific SCHEMA with 0. What is the easiest way of doing it, since I have many tables/columns?

Sample data:

CREATE TABLE [mySchema].[test1](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ParentID] [int] NULL,
    [ClassCode] [int] NULL,
    [ClassValue] [int] NULL,
 CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED ([ID] ASC)
) 

CREATE TABLE [mySchema].[test2](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ParentID] [int] NULL,
    [ClassCode] [int] NULL,
    [ClassValue] [int] NULL,
 CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED ([ID] ASC)
) 

INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,1,NULL)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,NULL,NULL)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,1,2)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,NULL,2)

INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,1,NULL)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,NULL,NULL)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,1,2)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,NULL,2)

Result should be:

Test1

+----+----------+-----------+------------+
| ID | ParentID | ClassCode | ClassValue |
+----+----------+-----------+------------+
|  1 |      100 |         1 |          0 |
|  2 |      100 |         0 |          0 |
|  3 |      100 |         1 |          2 |
|  4 |      100 |         0 |          2 |
+----+----------+-----------+------------+

Test 2

+----+----------+-----------+------------+
| ID | ParentID | ClassCode | ClassValue |
+----+----------+-----------+------------+
|  1 |      100 |         1 |          0 |
|  2 |      100 |         0 |          0 |
|  3 |      100 |         1 |          2 |
|  4 |      100 |         0 |          2 |
+----+----------+-----------+------------+

Thank you in advance.

Andrea
  • 11,801
  • 17
  • 65
  • 72
Chriz
  • 123
  • 12

2 Answers2

2

Below script will generate an update statement to update all null values with 0.

SELECT 'UPDATE ' + TABLE_NAME + ' SET [' + COLUMN_NAME + '] = 0 WHERE [' + 
COLUMN_NAME + '] IS NULL'
FROM INFORMATION_SCHEMA.COLUMNS
-- WHERE TABLE_NAME = 'YOURTABLENAME'

Uncomment the last line to do generate the script for a specific table.

Habeeb
  • 7,601
  • 1
  • 30
  • 33
  • Thank you for the answer, I have one more question though. This will create all the update statements, how will I execute them?Should I save it to a file and execute the script? – Chriz Jul 31 '18 at 07:48
  • Hi Chriz, you can run it in SSMS or any other Query Window or run via program. Your choice. – Habeeb Jul 31 '18 at 09:04
2

You can dynamically create the statement from SQL Server metadata table (information_schema.columns) filtering:

  • by schema (your_specific_schema in the code sample)
  • by datatype (probably numeric data types)

Here is a sample code:

declare @sql nvarchar(max) =''

select @sql = @sql + 'update [' + table_schema + '].[' + table_name + ']'
              + ' set [' + column_name + '] = 0 where [' + column_name + '] is null' 
              + char(10)
from information_schema.columns
where data_type in ('int','bigint') --ad all numeric types you want to set to 0
    and TABLE_SCHEMA = 'your_specific_schema'
print @sql

Output should look like this:

enter image description here

Once you have checked the generated script you can execute it:

  • with copy & paste and then with F5
  • or you can execute the statement directly from the variable @sql appending this statement to the query above:

    exec (@sql)
    
Andrea
  • 11,801
  • 17
  • 65
  • 72