Home » Interview Q & A » Q9. How do you rename a table or column name?

Q9. How do you rename a table or column name?

Our Categories

Blog Stats

  • 32,288 hits

Calendar

June 2013
M T W T F S S
« May    
 12
3456789
10111213141516
17181920212223
24252627282930

Please visit SQLVERSITY.COM for more details.

Syntax:

To rename a table name:

sp_RENAME ‘[PresentTableName]’,‘[NewTableName]’

GO

To rename a table column name:

sp_RENAME ‘TableName.[PresentColumnName]’,‘[NewColumnName]’,‘COLUMN’

GO

Ex:

sp_RENAME ‘Mytable’,‘Myrenamedtable’

GO

sp_RENAME ‘Myrenamedtable.Mycolumn’,‘Myrenamedcolumn’,‘COLUMN’

GO

Note:

After the executing any of the above rename commands, you will get the below warning.

Caution: Changing any part of an object name could break scripts and stored procedures.

Renaming an object may cause other objects. Because, some stored procedures or triggers or views might use this table in their code. So if we change the name, all the objects which are depending on it will be failed. So be careful while renaming an object 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: