Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

problem with naming default constraint and using it while adding a not null column Expand / Collapse
Author
Message
Posted Monday, November 5, 2007 2:10 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, June 24, 2011 9:55 AM
Points: 524, Visits: 219
i have a table 'staff'... it already contains some 10-15 records
now i want to add a new column StaffCode to the table which is not null.... iam adding a default value otherwise it will throw error

the syntax is:
ALTER TABLE Staff ADD StaffCode varchar(10) DEFAULT ' ' NOT NULL;
GO

iam able to create a default value and add the column but i want to change the name of default constraint... how to do it...

i tried the following steps

ALTER TABLE Staff ADD StaffCode varchar(10)NULL;
GO

ALTER TABLE Staff ADD CONSTRAINT Staff_StaffCode_df DEFAULT ' '
FOR StaffCode
GO

ALTER TABLE Staff ALTER COLUMN StaffCode varchar(10) NOT NULL;
GO

I get the error:-
Msg 4901, Level 16, State 1, Line 1
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 'StaffCode' cannot be added to non-empty table 'Staff' because it does not satisfy these conditions.

Please help.... iam able to give default value but not able to give my own constraint name
Post #418412
Posted Monday, November 5, 2007 2:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 5:52 AM
Points: 2,551, Visits: 2,594
ALTER TABLE Staff ADD StaffCode varchar(10) NOT NULL CONSTRAINT Staff_StaffCode_df DEFAULT(' ');

--Ramesh

Post #418419
Posted Monday, November 5, 2007 3:11 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, June 24, 2011 9:55 AM
Points: 524, Visits: 219
thanks... i was able to get it

Regards,

Amit kulkarni
Post #418438
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse