dbTalk Databases Forums  

Beginner question, stored procedure

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Beginner question, stored procedure in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Richard
 
Posts: n/a

Default Beginner question, stored procedure - 11-17-2007 , 09:24 PM






Hello,

I have 2 tables: Employees(EmployeeID, FirstName, LastName),
EmployeeApplication(CreatedByID, EmployeeID,Date1, Date2, Content...)
I need to create stored procedure that will return employee First and
Last names (plus some other columns) instead *both* ID's in
EmployeeApplication.
I know how to do it with only one - I am using simple join, but with
two I have no idea...
Can anyone help me ?

Richard...

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Beginner question, stored procedure - 11-17-2007 , 10:16 PM






Hi Richard,

You simply add one more join for the Employees table to get the names for
the other ID, something like this:

SELECT A.EmployeeID,
E.FirstName AS emp_fname,
E.LastName AS emp_lname,
A.CreatedByID,
C.FirstName AS creator_fname,
C.LastName AS creator_lname,
A.Date1,
A.Date2,
A.Content
FROM EmployeeApplication AS A
JOIN Employees AS E
ON A.EmployeeID = E.EmployeeID
JOIN Employees AS C
ON A.CreatedByID = C.EmployeeID

This is assuming the CreatedByID column is referring to the EmployeeID
column from the Employees table. If that is not the case then you have to
replace the Employees table in the second join with the appropriate table
and the appropriate column for the join.

HTH,

Plamen Ratchev
http://www.SQLStudio.com



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.