Our database is running on SQL Server 2012. I'd like to null out some unwanted data, rather than just delete the row. I can't null out all the columns in the table because some have "not null" defined. So, rather than manually specifying all the columns by name that allow null values in my update statement, I was hoping to find a way to either filter to it automatically, or try/catch based on whether it is nullable or errors out somehow. That way I can reuse this on any given table. Any ideas?
I can see that sys.columns
has a is_nullable
and is_computed
column that I can query, I just don't know how to wrap it all together in my update statement to say "Null out any column in this table that allows nulls".
Edit: just for example, I was testing with the AdventureWorks2014
database. I can run the below query to see which columns allow nulls and are not computed:
select name as 'column'
from sys.columns
where is_nullable = 1
and is_computed = 0
and object_id = OBJECT_ID('HumanResources.Employee')
Also, I can run the following query to null out some columns that I manually specify:
update [AdventureWorks2014].[HumanResources].[Employee]
set OrganizationNode = NULL
I just don't know how to combine them.