Solved

How do rename and remove columns in a MS SQL table?

Posted on 2010-09-15
3
766 Views
Last Modified: 2012-05-10
I'm using the Server Management Objects library to alter a table.

My code for adding a column works fine, but whenever I try to rename a column, I get "SetName failed for Column '[columnName]'."

Likewise, my attempts to remove a column are met with "The Column '[columnName]' does not exist on the server."

All I'm doing are:

      tbl.Columns[oldName].Name = newName;

      tbl.Columns.Remove(name);

Another error that I've gotten when I attempt to remove columns is "You cannot perform operation Remove on an object in state Existing."

Help!
0
Comment
Question by:MiloDCooper
  • 2
3 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
Hi,

If all you want is to remove a column name from a table, why not simply do this:

ALTER TABLE TableName
DROP COLUMN ColumnName
And if your intention is to rename a column from old to new, for instance, this will do it:

sp_RENAME Yourtable.OldName', 'NewName' , 'COLUMN'

That's it
Is it possible that I misunderstood what you are trying to do?
0
 

Accepted Solution

by:
MiloDCooper earned 0 total points
Comment Utility
OK I found that removing a column is achieved thusly:

      tbl.Columns[name].Drop();

@sammySeltzer: I'm looking for the .NET SMO solution, as opposed to relaying actual SQL commands.
0
 

Author Comment

by:MiloDCooper
Comment Utility
Aaaaaaaaand I've found that renaming is achieved like so:

      tbl.Columns[oldName].Rename(newName);

K, so, never mind I guess.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now