Solved

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

Posted on 2010-09-15
3
775 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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diagnostics with Net and Net.Sockets 2 26
Problem!!! 8 33
SQL Help 27 45
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 32
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…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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