Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 29
PROPERCASE SCRIPT IN SQL 3 16
Why I am having this error in my query? 2 25
Are triggers slow? 7 12
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

839 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