Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Include select list AND results in query results

Author  Topic 

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-05-11 : 11:31:01
I have a list of employee first names and last names given to me in a spreadsheet. I wanted to take them and put them in a query like this:
SELECT LAST_NAME, FIRST_NAME, EMPLID, REPORTS_TO_MGR_NAME, REPORTS_TO_MGR_EMPLID
FROM _sde.v_HR
WHERE ( LAST_NAME = 'Adams' AND FIRST_NAME = 'John ' )
OR ( LAST_NAME = 'Jackson' AND FIRST_NAME = 'Andrew' )
OR ( LAST_NAME = 'Lincoln' AND FIRST_NAME = 'Abraham' )
OR ( LAST_NAME = 'Washington' AND FIRST_NAME = 'George' )
ORDER BY LAST_NAME, FIRST_NAME

The problem is that I would like to include the LAST_NAME, FIRST_NAME which are in the select list, whether or not there are any results. Also, there are sometimes more than one of a certain LAST_NAME, FIRST_NAME. These are already being pulled and I don't want to suppress that, obviously. I just want the LAST_NAME, FIRST_NAME and NULL columns for any that do not exist in v_HR.

Thank you.

Duane

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2010-05-11 : 12:05:47
Try this:

DECLARE @EmployeesList TABLE
(
last_name VARCHAR(100) NOT NULL,
first_name VARCHAR(100) NOT NULL
);

INSERT INTO @EmployeesList(last_name, first_name)
SELECT 'Adams', 'John ' UNION ALL
SELECT 'Jackson', 'Andrew' UNION ALL
SELECT 'Lincoln', 'Abraham' UNION ALL
SELECT 'Washington', 'George';

SELECT EL.last_name, EL.first_name, HR.emplid, HR.reports_to_mgr_name, HR.reports_to_mgr_emplid
FROM @EmployeesList AS EL
LEFT OUTER JOIN
_sde.v_HR AS HR
ON HR.last_name = EL.last_name
AND HR.first_name = EL.first_name
ORDER BY ET.last_name, ET.first_name
Go to Top of Page

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-05-11 : 16:46:58
Thank you. This worked fine. And I already had decided to try to use variables. You saved me the trouble on that, too.

Duane
Go to Top of Page
   

- Advertisement -