Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2005
    Location
    San Francisco, CA
    Posts
    506

    Unhappy Unanswered: Changing Field name of an existing Table

    Hi,
    Is it possible to change any fieldname of an existing table?I mean to say
    by TSQL statement.We know that we can alter the data type and width etc.
    But I haven't got any info about filedname change.So if it is possible
    Please help...
    And Is there any TSQL command to alter multiple columns in a single statement?

    Please help...
    Thanks!!
    Joydeep
    Last edited by rudra; 01-17-06 at 16:07.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    if it is not a primary or a foriegn key you can do something like so...

    ALTER TABLE Mytable
    ADD INQUIRYID2 BIGINT
    GO
    UPDATE MyTable SET INQUIRYID2 = INQUIRYID
    GO
    ALTER TABLE MyTable
    DROP COLUMN INQUIRYID

    But this a bad idea. What about the existing database object that reference the existing column. Oh well too late. A thousand application buga have just been created. Update your resume. You want biggie size
    “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 2003
    Location
    India
    Posts
    216
    if you can connect the db thru EM, just type the new name.....

  4. #4
    Join Date
    Jul 2003
    Location
    The Dark Planet
    Posts
    1,401
    Quote Originally Posted by upalsen
    if you can connect the db thru EM, just type the new name.....
    The Holy Book says ...
    Code:
    sp_rename
    
    Changes the name of a user-created object (for example, table, column, or user-defineddata type) in the current database.
    Syntax
    
    sp_rename [ @objname = ] 'object_name' , 
        [ @newname = ] 'new_name' 
        [ , [ @objtype = ] 'object_type' ]
    Arguments
    
    [@objname =] 'object_name'
    Is the current name of the user object (table, view, column, stored procedure, trigger, default, database, object, or rule) or data type. If the object to be renamed is a column in a table, object_name must be in the form table.column. If the object to be renamed is an index, object_name must be in the form table.index. object_name is nvarchar(776), with no default.
    [@newname =] 'new_name'
    Is the new name for the specified object. new_name must be a one-part name and must follow the rules for identifiers. newname is sysname, with no default.
    [@objtype =] 'object_type'
    Is the type of object being renamed. object_type is varchar(13), with a default of NULL, and can be one of these values.
    ValueDescriptionCOLUMNA column to be renamed.DATABASEA user-defined database. This option is required when renaming a database.INDEXA user-defined index.OBJECTAn item of a type tracked in sysobjects. For example, OBJECT could be used to rename objects including constraints (CHECK, FOREIGN KEY, PRIMARY/UNIQUE KEY), user tables, views, stored procedures, triggers, and rules.USERDATATYPEA user-defined data type added by executing sp_addtype.
    I don't know why you are providing a round about way of doing the same thing.
    Get yourself a copy of the The Holy Book

    order has no physical Brett in The meaning of a Kaiser . -database data

  5. #5
    Join Date
    Jul 2006
    Posts
    1

    Lightbulb Try sp_rename

    You can try out this one.


    EXECUTE sp_rename N'MyTable.MyCurrFName', N'MyNewFName', 'COLUMN'


    MyTable has a Field MyCurrFName. It will be renamed to MyNewFName

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    You are better off either a). Create a view that has the name you want, or b). unloading, dropping, create, and load

    There was a very good article that discusses why do the alter causes sql server to waste a lot of space on the data pages....Now if I can find on Nigel's web site I'll post a link
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  7. #7
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  8. #8
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    thanks Brett, thats really a good link. never thought of it.

Posting Permissions

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