User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 373,936 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,491 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 2672 | Replies: 2
Reply
Join Date: Sep 2006
Posts: 104
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster

How to add not null column in SQL Server 2005

  #1  
Apr 7th, 2008
I have to add a not null column in a table.
I am using

IF NOT EXISTS(SELECT * FROM information_schema.columns WHERE column_name = 'xyz' 
AND table_name = 'abc') 	
BEGIN
	ALTER TABLE abc	WITH NOCHECK ADD xyz MONEY NOT NULL
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 5:25 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 743
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 3
Solved Threads: 53
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

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

  #2  
Apr 7th, 2008
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.
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

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

  #3  
Apr 15th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 6:40 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC