943,816 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 29120
  • MS SQL RSS
Apr 7th, 2008
0

How to add not null column in SQL Server 2005

Expand Post »
I have to add a not null column in a table.
I am using

MS SQL Syntax (Toggle Plain Text)
  1. IF NOT EXISTS(SELECT * FROM information_schema.COLUMNS WHERE column_name = 'xyz'
  2. AND table_name = 'abc')
  3. BEGIN
  4. ALTER TABLE abc WITH NOCHECK ADD xyz MONEY NOT NULL
  5. END

It gives me error
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'xyz' cannot be added to non-empty table 'abc' because it does not satisfy these conditions.

can anybody have any idea, what's wrong with my SP?


?
Last edited by peter_budo; Apr 7th, 2008 at 6:25 am. Reason: Keep It Organized - please use [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
bhavna_816 is offline Offline
116 posts
since Sep 2006
Apr 7th, 2008
0

Re: How to add not null column in SQL Server 2005

You can't, simply when you add a column all rows before this adding have this column = null, so you can remove all rows and add a non-nullable column.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 15th, 2008
0

Re: How to add not null column in SQL Server 2005

if you want to add a not null column to a table with rows, you need to set a default value to the new column.

adding a new column without a default value would put a null in the column for each row, which would be breaking the not null rule.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
May 11th, 2010
0
Re: How to add not null column in SQL Server 2005
U cant add a not null column using ALTER command.Because it allows
to add only null columns.If u want to add datas into that null column you need to use UPDATE command for adding data into that table
Like,

UPDATE TABLE table_name SET null_column-name='value' WHERE column_name='value

EX
UPDATE TABLE employee SET add1='SARA' WHERE sno=1
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saranya_kamaraj is offline Offline
1 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: problem on inserting an value into stored procedure
Next Thread in MS SQL Forum Timeline: SQL with Case Statements





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC