2

I've created a table column called RateValue with initially allowing NULL value but I want to make it NOT NULL. I used SQL Server 2008 R2

I tried the following but it does not work

ALTER TABLE dbo.AAElement
ALTER COLUMN RateValue NVARCHAR(50) NOT NULL
4
  • 2
    Do you have any rows in this table? If you do you must first make sure that you have no row with a NULL value in RateValue. Commented Aug 28, 2013 at 12:34
  • 2
    what's the error message you get ? Commented Aug 28, 2013 at 12:37
  • possible duplicate of Altering a column: null to not null Commented Aug 28, 2013 at 12:38
  • "Not working" usually means "you did not read the message". Read it because it tells you what is wrong. Commented Aug 28, 2013 at 12:44

1 Answer 1

4

The most likely problem is pre-existing NULL values.

Get rid of the NULL values first, then alter:

UPDATE AAElement 
SET RateValue = ''
WHERE RateValue IS NULL
GO
ALTER TABLE dbo.AAElement
ALTER COLUMN RateValue NVARCHAR(50) NOT NULL

Otherwise the constraint is violated as it's being created.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.