Click here to Skip to main content
15,868,419 members
Articles / Programming Languages / SQL
Tip/Trick

SQL Server Function - SP_RENAME and NEWID

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
1 Dec 2012CPOL1 min read 15.3K   2   2
Getting overview of new SQL Server functions

Today, we will have an overview of two SQL Server functions

  • SP_RENAME
  • NEWID


SP_RENAME is used to rename table name or rename any column of the table.
NEWID is used to extract random records from table.

Renaming of Column in Table  

Syntax  
sp_RENAME 'Tablename.ColumnName','NewColumnName','Column' 

I had a table named as Employee_Salary in my SQL Server 2012 under Northwind database. It has four columns ID (Primary Key), Employee_Name,Salary and Sal_Month. 

I want to rename column named as 'Employee_Name' to 'Employees_Name' 

So, below is the command that needs to be executed

sp_RENAME 'Employee_Salary.Employee_Name','Employees_Name','Column'

Once done, you will see that column name would be updated.

Renaming of Table  

We can rename the table with sp_RENAME function provided in SQL Server.

Syntax 

sp_RENAME 'TableName' , 'TableNewName'
Below is the command executed
sp_RENAME 'Employee_Salary','Employees_Salary'

NEWID() Function     

As told, NEWID() is used to extract random records from the table.

We had table named as Employees_Salary containing employees salary for all the months. Below are the records present in the table 

 

Below is the query to extract random records

select top 2 * from Employees_Salary order by NEWID() 
Below is the data extracted when executed the query

 

If we execute the above query again below is the data returned

 

So, you have seen every time we execute the same query we are getting different records from table.
Hope with this article of mine, you have become familiar with very little but important functions of SQL Server. 

History

Keep a running update of any changes or improvements you've
made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Please visit my blog http://varunkhanna.blogspot.com/ to read more articles

Comments and Discussions

 
GeneralMy vote of 1 Pin
Rob Eckert2-Dec-12 3:42
Rob Eckert2-Dec-12 3:42 
GeneralMy vote of 1 Pin
Violet Tape1-Dec-12 22:57
Violet Tape1-Dec-12 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.