Update all SQL NULL values in multiple columns using Column level WHERE clause?

前端 未结 4 1863
花落未央
花落未央 2021-01-02 18:31

We have a database with a bunch of wide tables (40-80 columns each) and just found a bug that introduced NULL values into about 500 of the records. The NULL values can appea

4条回答
  •  梦毁少年i
    2021-01-02 19:27

    Just poll the sys.columns table for each table and create some dynamic sql... It's brute force but it saves you from having to write all the t-sql out.

    For example:

    DECLARE @TABLENAME AS VARCHAR(255)
    
    SET @TABLENAME = 'ReplaceWithYourTableName'
    
    SELECT 'UPDATE ' + @TableName  + ' SET ' + CAST(Name AS VARCHAR(255)) + ' = 99 
     WHERE ' + CAST(Name AS VARCHAR(255)) + ' IS NULL'
    FROM sys.columns 
    WHERE object_id = OBJECT_ID(@TABLENAME)
        AND system_type_id = 56 -- int's only
    

提交回复
热议问题