Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
2.75/5 (3 votes)
See more:
I have three table as follow

-------
table name Person
----------
PersonId
HomeId
FirstName
Email
Gender
Password

====================================
table name NoticeBoard
-------------------------
NoticeBoardId
NoticeTitle
Notice
CreationDate
PersonId

=====================================
table name Home
-------
HomeId
HomeNumber
HomeName


I want to make search sql query...
as like..
If I want to find person name, Person HomeNumber and NoticeTitle so how to find following query can not work..

SQL
ALTER PROCEDURE dbo.sp_Custom_Select_Complain
(
	@PersonId int = NULL,
	@FirstName varchar(50) = NULL,
	@Gender varchar(50) = NULL,

	@ComplainId int = NULL,
	@ComplainTitle varchar(100) = NULL,
	@ComplainInfo varchar(400) = NULL,
	
	@HomeId int = NULL,
	@HomeNumber int = Null,
	@HomeName varchar(50) = Null

)
AS
select 
	P.FirstName,
	P.Gender,

	C.ComplainTitle,
	C.ComplainInfo,

	H.HomeNumber,
	H.HomeName

From Person P 
	Inner Join Complain C On C.PostId = P.PostId
	Inner Join Home H On H.HomeId = P.HomeId

Where 
	(@PersonId Is Null Or P.PersonId = @PersonId)
AND	(@FirstName Is Null Or P.FirstName LIKE '%' + @FirstName + '%')
AND	(@Gender Is Null Or P.Gender = @Gender) 

AND	(@ComplainTitle Is Null Or C.ComplainTitle LIKE '%' + @ComplainTitle + '%') 
AND	(@ComplainInfo Is Null Or C.ComplainInfo LIKE '%' + @ComplainInfo + '%') 

	RETURN




----------------------------------------------
please help me...
Posted
Updated 10-Mar-14 23:57pm
v2
Comments
Fredrik Bornander 11-Mar-14 4:59am    
What does your Complain table look like?
ZurdoDev 11-Mar-14 7:34am    
A quick glance makes me think it should work right. What isn't working about it?
Charlemagne Gustilo 13-Mar-14 21:17pm    
Have you tried changing the AND conditions to OR?

Try changing your joins to left outer joins
 
Share this answer
 
Hi,

did u check null value passing to integer value.

Example :

int? PersonId = Null

OR

int? PersonId = 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900