Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Store procedure with 8 optional parametes, if the optinal parameter passes when calling the store procedure it'll add the condition to the Query according to the parameter value.

This is My Store Procedure

SQL
ALTER PROCEDURE [dbo].[Copy_Search_Candidate_forcampus] --15,12,8,'10th',80.00
(
@Course_id  bigint,
@Year_id  bigint,
@Semester_id  bigint,
@Criteria1 varchar(50)=null,
@Marks1 decimal(18,2)=null,
@Criteria2 varchar(50)=null,
@Marks2 decimal(18,2)=null,
@Criteria3 varchar(50)=null,
@Marks3 decimal(18,2)=null,
@Criteria4 varchar(50)=null,
@Marks4 decimal(18,2)=null
)

AS

BEGIN
set nocount on;
   if 1=0
   begin
   set fmtonly off
   end
BEGIN TRANSACTION

select a.*,
c.over_all_per MRes,
d.over_all_per HRes,
e.over_all_per DRes
from
admission_table1 a
join 
Branch_Master b on a.Branch_Id=b.Branch_Id
 left outer join
 admission_table2 c on a.student_id=c.student_id and c.exam='10th'
 left outer join 
 admission_table2 d on a.student_id=d.student_id and d.exam='12th'
 left outer join 
 admission_table2 e on a.student_id=e.student_id and e.exam='Diploma'
 where
a.Course_Id=@Course_id and 
a.Semester_Id=@Semester_id and
a.Year_Id=@Year_id 
and c.over_all_per>=80


COMMIT

IF @@ERROR<>0
    begin
	ROLLBACK
    end
END


I want It Like This
SQL
select a.*,
c.over_all_per MRes,
d.over_all_per HRes,
e.over_all_per DRes
from
admission_table1 a
join 
Branch_Master b on a.Branch_Id=b.Branch_Id
 left outer join
 admission_table2 c on a.student_id=c.student_id and c.exam='10th'
 left outer join 
 admission_table2 d on a.student_id=d.student_id and d.exam='12th'
 left outer join 
 admission_table2 e on a.student_id=e.student_id and e.exam='Diploma'
 where
a.Course_Id=@Course_id and 
a.Semester_Id=@Semester_id and
a.Year_Id=@Year_id 
 +++ADD HERE

if Criteria1 <>null +++    and c.over_all_per>@Marks1
if Criteria2 <>null +++    and d.over_all_per>@Marks2
if Criteria3 <>null +++    and e.over_all_per>@Marks3


If you don't understand the Question then Ask me in comments, i'm on.
actually i'm not a Good Describer.

Another thing, I know the Lengthy process to Solve it, by Checking all the parameter value and writing the query.


[edit]
i think you all didn't get my question clearly.

here Criteria1,2,3,4 is just the optional parameter and c,d,e.over_all_per is the same column from a single table.

and my condition is..
if Criteria2 <>null then d.over_all_per>@mark2 condition should apply +
if Criteria3 <>null then e.over_all_per>@mark3 condition should apply+
if Criteria4 <>null then f.over_all_per>@mark4 condition should apply

Student_table withh student_id PK Column
and Student_Exam with student_id FK column,exam,Mearks
here we checking on Student_Exam table on Student_id FK fro exam[Criteria1,2,3,4] and Marks[marks1,2,3,4]
Posted
Updated 14-Nov-14 11:43am
v5
Comments
Shweta N Mishra 14-Nov-14 10:49am    
i guess you are expecting that if Criteria1 <>null then only c.over_all_per>@mark1 condition should apply, is it correct ?
/\jmot 14-Nov-14 10:55am    
yes. and
if Criteria2 <>null then d.over_all_per>@mark2 condition should apply +
if Criteria3 <>null then e.over_all_per>@mark3 condition should apply+
if Criteria4 <>null then f.over_all_per>@mark4 condition should apply

You could do something like this:

SELECT a.*,
	c.over_all_per MRes,
	d.over_all_per HRes,
	e.over_all_per DRes
FROM
	admissiON_table1 a
JOIN 
	Branch_Master b ON a.Branch_Id=b.Branch_Id
	LEFT OUTER JOIN
		admissiON_table2 c ON a.student_id=c.student_id AND c.exam='10th'
	LEFT OUTER JOIN
		admissiON_table2 d ON a.student_id=d.student_id AND d.exam='12th'
	LEFT OUTER JOIN
		admissiON_table2 e ON a.student_id=e.student_id AND e.exam='Diploma'
WHERE
	a.Course_Id = @Course_id AND  
	a.Semester_Id = @Semester_id AND
	a.Year_Id = @Year_id AND 
	(Criteria = @Criteria1 OR @Criteria1 IS NULL) AND
	(Marks = @Marks1 OR @Marks1 IS NULL)
 
Share this answer
 
Comments
DamithSL 14-Nov-14 10:47am    
It is DiTto :P
5wd
try with
SQL
where
a.Course_Id=@Course_id and
a.Semester_Id=@Semester_id and
a.Year_Id=@Year_id and
(Criteria1 is null or c.over_all_per > @Marks1) and
(Criteria2 is null or d.over_all_per > @Marks2) and
(Criteria3 is null or d.over_all_per > @Marks3)
 
Share this answer
 
Comments
Manas Bhardwaj 14-Nov-14 10:42am    
Ditto +5!
/\jmot 14-Nov-14 11:08am    
i try, but not getting the result..
code is running,No error..
I usede it Like This..

where
a.Course_Id=@Course_id and
a.Semester_Id=@Semester_id and
a.Year_Id=@Year_id
and (@Criteria1 is not null or c.over_all_per > @Marks1)
--and (@Criteria2 is null or d.over_all_per > @Marks2) and
--(@Criteria3 is null or e.over_all_per > @Marks3)

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