Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2007
    Posts
    197

    Smile Unanswered: adding new column

    ALTER TABLE BFTITLE ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL ;

    syntax is not working so can any one tell the right syntax

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Code:
    alter Table Bftitle Add [ratetype] [numeric] (10) Not Null
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Read BOL (SQL Server Books On Line) carefully. If a table already has rows in it, it isn't possible to add a NOT NULL column unless you also give the column a default value too (if you think about it, this only makes sense). I'm guessing on this, but I'd suggest trying:
    Code:
    ALTER TABLE BFTITLE
       ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL
       DEFAULT 0
    -PatP

  4. #4
    Join Date
    Dec 2002
    Posts
    1,245
    Quote Originally Posted by Pat Phelan
    Code:
    ALTER TABLE BFTITLE
       ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL
       DEFAULT 0
    -PatP
    Close. This worked for me:

    Code:
    ALTER TABLE BFTITLE
       ADD [RATETYPE] [NUMERIC] (10) NOT NULL
       DEFAULT 0
    I just removed "COLUMN" from your statement. Funny, I did not think it would be possible to add a non-nullable column, but you're right it can be done as long as you define a default value.

    Following is proof:

    Code:
    create table foo (bar int)
    go
    
    insert foo (bar) values (1)
    go
    insert foo (bar) values (2)
    go
    insert foo (bar) values (3)
    go
    insert foo (bar) values (4)
    go
    
    ALTER TABLE foo
       ADD [RATETYPE] [NUMERIC] (10) NOT NULL
       DEFAULT 0
    
    
    select * from foo
    
    
    sp_columns foo
    Have you hugged your backup today?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •