In this blog I will show you how to rename a column name in sql. The syntax for renaming the column is
@objname = 'table_name.old_column_name',
@newname = 'new_column_name',
@objtype = 'COLUMN'
According to the msdn:
"sp_rename" Changes the name of a user-created object in the current database. This object can be a table, index, column, alias data type, or Microsoft .NET Framework common language runtime (CLR) user-defined type.
Below is the sample script which will explain you how to rename a column name .
IF EXISTS ( SELECT *
WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[TEST_COLUMNRENAME_TABLE]')
AND OBJECTPROPERTY(OBJECT_ID, N'IsUserTable') = 1 )
DROP TABLE [DBO].[TEST_COLUMNRENAME_TABLE]
CREATE TABLE TEST_COLUMNRENAME_TABLE
INSERT INTO [TEST_COLUMNRENAME_TABLE]
--NOW WE WILL CHANGE THE NAME OF COLUMN [FIRSTNAME] TO [LASTNAME]
EXEC SP_RENAME @objname = 'TEST_COLUMNRENAME_TABLE.FIRSTNAME',
@newname = 'LASTNAME', @objtype = 'COLUMN'
So from the output as you have seen the column name has been renamed.
Do let me know your feedback,comments.
In this blog I will help you how to delete duplicate rows from a table where there is no primary key in the table. Below is the script to insert data: CREATE TABLE MyTabl...
by: Raghav Khunger
While working with our application I wanted to list all the stored procedures having some particular text. I decided to share the query which I used to do that: SELECT * ...
Below is the SQL query to get all constraints: SELECT TableName = t . Name , ColumnName = c . Name , dc . Name , dc . definition FROM sys . tables t INNER JOIN sys . defa...
Below is the query which you can use to shrink the transcation log: USE AdventureWorks2008R2 ; GO -- Truncate the log by changing the database recovery model to SIMPLE. A...
If you want to search a string|text in all the tables, rows, columns below script will help you
While working with SQL Server 2008 R2 I was getting the following error: "Saving Changes Is Not Permitted On SQL Server 2008" I am writing the steps how you can fix it: C...
What kind of email newsletter would you prefer to receive from CodeAsp.Net?