Q

Manipulate column names in a SQL Server table

Learn SQL Server commands to manipulate column names in a table and turn column names into variables or rename SQL columns.

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.
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

Dig deeper on SQL-Transact SQL (T-SQL)

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close