How to alter a table with not null columns?

  • Hi,

    I want to add two columns, VARCHAR and DATETIME, to an existing table with data, that are NOT NULL columns.

    ALTER TABLE ADDRESS

    ADD DATE_LAST_UPDATED DATETIME DEFAULT ??? NOT NULL,

    USER_LAST_UPDATED VARCHAR(8) DEFAULT 'USER' NOT NULL

    It works for USER_LAST_UPDATED.

    How do I do it for DATETIME?

    Can anyone tell me if it is possible to add a not null datetime column to an existing table with data?

    Thanks

  • replace ??? with some date ('1/1/1900'). that should add your columns.

  • I used "1/1/1900", maybe it only accepts single quotes then. I'll give it a try. Thanks.

  • hengert (8/20/2008)


    I used "1/1/1900", maybe it only accepts single quotes then. I'll give it a try. Thanks.

    yes you need to use single quotes

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • hengert (8/20/2008)


    Hi,

    I want to add two columns, VARCHAR and DATETIME, to an existing table with data, that are NOT NULL columns.

    ALTER TABLE ADDRESS

    ADD DATE_LAST_UPDATED DATETIME DEFAULT ??? NOT NULL,

    USER_LAST_UPDATED VARCHAR(8) DEFAULT 'USER' NOT NULL

    It works for USER_LAST_UPDATED.

    How do I do it for DATETIME?

    Can anyone tell me if it is possible to add a not null datetime column to an existing table with data?

    Thanks

    To default your user_last_updated column, you'd better check these user related functions !!

    SELECT USER_ID() AS [USER_ID]

    , USER_NAME() AS [USER_NAME]

    , SUSER_ID() AS [SUSER_ID]

    , SUSER_SNAME() AS [SUSER_SNAME]

    , ORIGINAL_LOGIN() AS [ORIGINAL_LOGIN]

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 5 posts - 1 through 4 (of 4 total)

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