Ask the Expert

Manipulate column names in a SQL Server table

I have two questions regarding how to manipulate column names in SQL Server. Is there a command to turn the names of columns in a table into variables, and is there a command to rename columns, as well? I have attempted the latter, but it does not work in conjunction with Visual Basic programming.

    Requires Free Membership to View

You can use the following query to get a list of columns for a given table and use it to populate variables:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'Address'

You can rename a column using the following example:

EXECUTE sp_rename N'dbo.aaa.NewColumnName', N'NewColumnName2', 'COLUMN'

You can execute it from VB.NET, as well, by using the SqlCommand object and setting the CommandText property to the SQL code you want to execute. For a similar example, look at this expert answer on restoring SQL Server databases in VB.NET.

This was first published in October 2008

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: