Solved

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

Posted on 2010-09-15
3
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 33686218
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
ID: 33686298
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
ID: 33686335
Aaaaaaaaand I've found that renaming is achieved like so:

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

K, so, never mind I guess.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Simple Linear Regression
Six Sigma Control Plans

687 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