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 2000 Forums
 SQL Server Development (2000)
 sql join

Author  Topic 

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2006-12-27 : 05:51:37
Hi,
There are three tables:

tblUsers --> 28 records
tblDepartments
tblDepartmentsUsers --> something like a junction table between the above two tables.

I have entered several test records into tblDepartmentsUsers.
So that some of the users have Departments allocated to them.

I would like to have a query so that you can pass in a departmentID. The result should show all users in the users table. In addition, it should show the departmentID for the users that do have a department and null for the unacclocated users.

This the stored procedure that I have.
The problem is the where clause. because when I pass in the departmentID it returns only those allocated departments. Whereas I would like to see all users (null for users with unAllocated departmentID)

ALTER PROCEDURE [dbo].[DepartmentUsers_Get]

@DepartmentID int

AS

select
u.UserID,
u.UserName,
du.Percentage
from
Users as u left join DepartmentsUsers as pu on u.UserID = du.UserID
where
du.DepartmentIDID = @DepartmentID

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-12-27 : 06:01:38
Give this a go:

select
u.UserID,
u.UserName,
du.Percentage
from
Users as u left join DepartmentsUsers as pu on u.UserID = du.UserID
and
du.DepartmentIDID = @DepartmentID

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2006-12-27 : 06:12:50
Solved.
Thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-01-01 : 03:06:12
To know the difference read this
http://www.sqlteam.com/item.asp?ItemID=11122

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -