Solved

Combine data bases on parameters

Posted on 2009-05-18
2
216 Views
Last Modified: 2012-05-07
I have this sp below...if one of the fields is not null, all data is returned based on that parameter. If 2 parameters are not null, then only data fitting those non null parameters should eb returned and if 3 parameters, etc...as above with 2.  What I'm getting is data for only one parameter even when I input 2.  

If then statement?

Thx

ALTER PROCEDURE [dbo].[usp_getPOC1]

@UserID int = NULL,
@CenterID int = 1,
@ProjectID int = 1

AS

SELECT c.Center_Desc, p.Project, u.UserID, r.Last_Name, r.First_Name, r.Office_Phone, u.User_Email from dbo.tbl_User u
Join dbo.tbl_Registration r on u.UserID = r.UserID, dbo.LU_Center_Site c, dbo.LU_Project p
Where
@UserID is not null and @UserID = u.UserID and u.POC = '1' or
@CenterID is not NULL and @CenterID = c.CenterID or
@ProjectID is not null and @ProjectID = p.ProjectID
ORDER BY r.Last_Name
0
Comment
Question by:Glen_D
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 24413150
try this:


ALTER PROCEDURE [dbo].[usp_getPOC1]
 
@UserID int = NULL,
@CenterID int = 1,
@ProjectID int = 1
 
AS
 
SELECT c.Center_Desc, p.Project, u.UserID, r.Last_Name, r.First_Name, r.Office_Phone, u.User_Email from dbo.tbl_User u
Join dbo.tbl_Registration r on u.UserID = r.UserID, dbo.LU_Center_Site c, dbo.LU_Project p
Where u.POC = '1' AND --this always requires u.POC to = '1' -- which I think is what you mean.
(@UserID is null OR @UserID = u.UserID) and 
( @CenterID is NULL OR @CenterID = c.CenterID) AND
(@ProjectID is null OR @ProjectID = p.ProjectID)
ORDER BY r.Last_Name

Open in new window

0
 

Author Closing Comment

by:Glen_D
ID: 31582614
Perfect...thx Much  Glen
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now