Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a recordID in my SQL select query

I have a simple stored procedure that returns records:

Select empID, EmpFirstName, EmpLastName from EMPLOYEES  

The returned result is:

    EmpID         EmpFirstName       EmpLastName  
    -----         ------------       -----------  
   30152          John               Smith  
   30114          Tom                Jones  
   56332          Steve              Williams  
   85442          Paul               Johnson  

What I need is:

 RecID       EmpID         EmpFirstName       EmpLastName  
 -----       -----         ------------       -----------  
 1      30152          John               Smith  
 2      30114          Tom                Jones  
 3      56332          Steve              Williams  
 4      85442          Paul               Johnson  

How can I get the recordID column?

Thanks

like image 430
DNR Avatar asked Aug 09 '11 22:08

DNR


1 Answers

Probably want to use Row_Number:

Select ROW_NUMBER() OVER(ORDER BY empID) AS RecId, 
       empID, 
       EmpFirstName, 
       EmpLastName 
from EMPLOYEES 

unless there is actually a RecId column in EMPLOYEES in which case it would just be this:

Select RecId, 
       empID, 
       EmpFirstName, 
       EmpLastName 
from EMPLOYEES 
like image 153
Abe Miessler Avatar answered Oct 13 '22 10:10

Abe Miessler