Change a column to not allow nulls
Clearly, the table has NULL
values in it. Which you can check with:
select *from mydatabasewhere WeekInt is NULL;
Then, you can do one of two things. Either change the values:
update mydatabase set WeekInt = -1 where WeekInt is null;
Or delete the offending rows:
delete from mydatabase where WeekInt is null;
Then, when all the values are okay, you can do the alter table
statement.
If you are trying to change a column to not null, and you are getting this error message, yet it appears the column has no nulls, ensure you are checking for is null
and not = null
(which gives different results).
Select * from ... where column is null
instead of
Select * from ... where column = null
I am adding this because it tripped me up and took a while to resolve.
This will work. You should send a default value, then it will change all the previous record to -1 in this example.
alter table [dbo].[mydatabase] alter column WeekInt int not null DEFAULT '-1';