SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

Alter SQL Column

Last post 08-22-2008, 15:55 by WhoKnowsWhat. 5 replies.
Sort Posts: Previous Next
  •  08-22-2008, 8:38 14201

    Alter SQL Column

    I need to write n script to alter a column in a table that is already in use .

    The column looks like this: ColumnName (DateTime, not null)

    But I need to add n default value of '1900\01\01' and update all the rows in the column that do not have a value in it already for this column.

    Please help! Embarrassed

  •  08-22-2008, 9:25 14221 in reply to 14201

    Re: Alter SQL Column

    The T-SQL to add the default looks like this

    ALTER TABLE <tablename>

    ADD CONSTRAINT <constraintname>

    DEFAULT '1900/01/01' for <columnname>

    Since the column is already not null there should not be any rows without a value in it so there should be no reason to run an update afterwards.


    Robert MacLean
    http://www.sadev.co.za
  •  08-22-2008, 9:30 14222 in reply to 14201

    Re: Alter SQL Column

    Hi there,

    I think this should solve your problem quite nicely:

    DECLARE @DoesExist INT

    SET @DoesExist = 0

    SELECT

    @DoesExist = ISNULL(SC.id, 0)

    FROM

    sysobjects so

    LEFT JOIN

    syscolumns SC

    ON

    So.id = sc.id

    WHERE

    sc.name like 'columnname'

    IF (@DoesExist <= 0)

    ALTER TABLE table_name ADD columnname DATETIME DEFAULT '1900/01/01' NOT NULL

    GO

    This will then add the new column, and create it with a default date as you require. The code before it will check that the column does not already exist in the table, so as to avoid errors. DBA's can be picky about these things Wink

     

    Hope it helps,

    Regards,

    The H.............................


    The Question is the Answer, and the Answer is the Question!
  •  08-22-2008, 9:31 14223 in reply to 14201

    Re: Alter SQL Column

    WhoKnowsWhat:

    The column looks like this: ColumnName (DateTime, not null)

    But I need to add n default value of '1900\01\01' and update all the rows in the column that do not have a value in it already for this column.

    How can there be rows that do not already have a value when the column is set to be "not null"? Hmm


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  08-22-2008, 15:47 14257 in reply to 14223

    Re: Alter SQL Column

    Heat_Rash:
    How can there be rows that do not already have a value when the column is set to be "not null"? Hmm

    I think the OP wants it to be not null, but can't make it so because there are existing rows with null.


    Unscrambling Eggs: Decompiling ASP.NET
  •  08-22-2008, 15:55 14262 in reply to 14201

    Re: Alter SQL Column

    Hey all,

    Sorry about the whole not null thing. I didn;t think properly - duh!

    Thanx for the responses

    Keep it up!

     

View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems