How to add a field to a table

  • Now that my database is in SQL Server, it seems it is very hard to add or modify a table.

    I don't have SQL Server Enterprise Manager, so I guess I need to use stored procedures?

    I need to add a field to a table, a numeric integer field but I haven't been able to find any examples that work.

    Can someone point me in the right direction?

     


    Kindest Regards,

    Jon
    Free Cell Phone Games

  • Even if you had Enterprise Manager, it is more efficient to add the field using code:

    alter table myTable add myNewColumn int null

     

    Aunt Kathi Data Platform MVP
    Author of Expert T-SQL Window Functions
    Simple-Talk Editor

  • I usually use vb6's data view window(from view menu) for that case.

     

    Rgds,

    Myint Thu

  • Try this:

    --NOTE: Change TableName and ColumnName to respective values.

    IF NOT (SELECT object_id('dbo.TableName') as idnum)IS NULL

    BEGIN

    IF NOT exists(SELECTt o.* from dbo.sysobjects o JOIN syscolumns c ON c.Id = o.Id WHERE o.id = object_id(N'[TableName]') and OBJECTPROPERTY(o.id, N'IsUserTable') = 1 BEGIN

    ALTER TABLE TableName ADD

    [ColumnName] integer NULL

    ALTER TABLE dbo.TableName ADD CONSTRAINT

    /* Uncomment the following statment if you wish to add a constraint

    DF_TableName_ColumnName DEFAULT 0 FOR ColumnName

    */

    END

     

    END

    GO

  • Should these 2 lines work by themselves?

    ALTER TABLE tblAccount ADD

    [MailServer] varchar(50)

     

     


    Kindest Regards,

    Jon
    Free Cell Phone Games

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

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