Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to create a stored procedure to search in two dropdownlist separatly and also in both. eg if i want ot search with a 1st or 2nd dropdownlist it must show the results accoding to the 1st or 2nd dropdownlist or
also if i want to search whith both dropdowns must show the reasult accoding to the two drpdowns. pleas help

this is my stored-proc it works but it allows me to search with both dropdownlits but if i want search whith 1 dropdown it gives an error. pleas help if i want to search with either dropdown.

C#
ALTER PROCEDURE [dbo].[Find_Schools]
(	
	
	@Services varchar (100),
	@Schools varchar (250)
)
	
AS
BEGIN
SET NOCOUNT ON;
SELECT  CI.CHLD_REF,CI.CHLD_ID,CI.CHLD_NAME, CI.CHLD_SURNAME, CI.CHLD_BIRTH_DATE,CI.CHLD_SchoolID, s.SVC_NAME, cs.CSVC_STATUS, cs.CSVC_UPDATE, cs.CSVC_EMPL_DN, E.Name, 
                      E.Surname,cs.CSVCID,cs.CSVC_SVC_ID
FROM         CHILD_SERVICE1 AS cs INNER JOIN
                      CHILD_INFO AS CI ON cs.CSVC_CHLD_ID = CI.CHLD_ID INNER JOIN
                      SERVICES AS s ON cs.CSVC_SVC_ID = s.SVC_ID INNER JOIN
                      EmpUsers AS E ON cs.CSVC_EMPL_DN = E.UserName
WHERE (cs.CSVC_REFERRED = 1 )and (s.SVC_NAME =@Services ) and (CI.CHLD_SchoolID = @Schools )
END
Posted
Updated 14-Feb-12 2:30am
v3
Comments
Keith Barrow 14-Feb-12 8:03am    
This is not just a stored procdure question, please tag the UI technology you are using because each will require a different answer.

You have two conditions so you would need two stored procs, or a mechanism in one to switch between the two conditions

1) dropdown1 OR dropdown2
2) dropdown1 AND dropdown2

What part do you need help with?
 
Share this answer
 
TRY THIS

SQL
ALTER PROCEDURE [dbo].[Find_Schools]
(	
	
	@Services varchar (100),
	@Schools varchar (250)
)
	
AS
BEGIN

DECLARE @Where VARCHAR(MAX)=''
DECLARE @SQL VARCHAR(MAX)

IF(@Services!='' AND @Schools!='')
BEGIN
	SET @Where+=' and (s.SVC_NAME ='''+@Services+''') and (CI.CHLD_SchoolID = '''+@Schools+''' )'
END
ELSE IF(@Services!='' AND @Schools='')
BEGIN
	SET @Where+=' and (s.SVC_NAME ='''+@Services+''')'
END
ELSE IF(@Services='' AND @Schools!='')
BEGIN
	SET @Where+=' and (CI.CHLD_SchoolID = '''+@Schools+''' )'
END

SET NOCOUNT ON;

SET @SQL='
SELECT  CI.CHLD_REF,CI.CHLD_ID,CI.CHLD_NAME, CI.CHLD_SURNAME, CI.CHLD_BIRTH_DATE,CI.CHLD_SchoolID, s.SVC_NAME, cs.CSVC_STATUS, cs.CSVC_UPDATE, cs.CSVC_EMPL_DN, E.Name, 
                      E.Surname,cs.CSVCID,cs.CSVC_SVC_ID
FROM         CHILD_SERVICE1 AS cs INNER JOIN
                      CHILD_INFO AS CI ON cs.CSVC_CHLD_ID = CI.CHLD_ID INNER JOIN
                      SERVICES AS s ON cs.CSVC_SVC_ID = s.SVC_ID INNER JOIN
                      EmpUsers AS E ON cs.CSVC_EMPL_DN = E.UserName
WHERE (cs.CSVC_REFERRED = 1 ) 
'+@Where+'
'

EXEC(@SQL)

END
 
Share this answer
 
v2
you add new variable in storeprocedure on this variable you alter your store procedure

SQL
@Qrytype int=1


if @Qrytype =1 -- when both passed
WHERE (cs.CSVC_REFERRED = 1 )and (s.SVC_NAME =@Services ) and (CI.CHLD_SchoolID = @Schools )

else if @Qrytype =2 -- when passed @Services 
WHERE (cs.CSVC_REFERRED = 1 )and (s.SVC_NAME =@Services )

else if @Qrytype =3 -- when passed @Schools 
WHERE (cs.CSVC_REFERRED = 1 )and (CI.CHLD_SchoolID = @Schools )
 
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