problem with naming default constraint and using it while adding a not null column

  • 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

  • ALTER TABLE Staff ADD StaffCode varchar(10) NOT NULL CONSTRAINT Staff_StaffCode_df DEFAULT(' ');

    --Ramesh


  • thanks... i was able to get it

    Regards,

    Amit kulkarni

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply